package com.zy.acs.manager.common.filter;
|
|
import org.springframework.core.Ordered;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.AntPathMatcher;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.util.ContentCachingRequestWrapper;
|
|
import javax.servlet.FilterChain;
|
import javax.servlet.ServletException;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
|
/**
|
* Wraps open-api requests so downstream interceptors can safely read the body multiple times.
|
*/
|
@Component
|
@Order(Ordered.HIGHEST_PRECEDENCE + 5)
|
public class IntegrationRequestCachingFilter extends OncePerRequestFilter {
|
|
private static final AntPathMatcher MATCHER = new AntPathMatcher();
|
private static final String[] LOG_PATTERNS = {"/api/open/**"};
|
|
@Override
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
throws ServletException, IOException {
|
HttpServletRequest wrapped = request instanceof ContentCachingRequestWrapper
|
? request
|
: new ContentCachingRequestWrapper(request);
|
filterChain.doFilter(wrapped, response);
|
}
|
|
@Override
|
protected boolean shouldNotFilter(HttpServletRequest request) {
|
String uri = request.getRequestURI();
|
for (String pattern : LOG_PATTERNS) {
|
if (MATCHER.match(pattern, uri)) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
@Override
|
protected boolean shouldNotFilterAsyncDispatch() {
|
return true;
|
}
|
|
}
|