#
vincentlu
1 天以前 0bee2b3f40638460f3fa961792d43570e4b46911
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
    }
 
}