如何设置OncePerRequestFilter的实现以仅过滤一个端点

时间:2018-08-21 13:45:04

标签: spring spring-mvc spring-boot

我正在使用此过滤器记录我的请求和响应,效果很好,但是我注意到我实际上并不需要所有端点使用此过滤器-它将更加高效并且足以过滤和记录来自一个端点的请求。 是否可以不使用afterRequest方法中的if语句? 我搜索了很多,但是几乎每个示例都具有spring security:(

@Slf4j
@Component
public class RequestAndResponseLoggingFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (isAsyncDispatch(request)) {
            filterChain.doFilter(request, response);
        } else {
            doFilterWrapped(wrapRequest(request), wrapResponse(response), filterChain);
        }
    }

    protected void doFilterWrapped(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response, FilterChain filterChain) throws ServletException, IOException {
        beforeRequest(request, response);
        try {
            filterChain.doFilter(request, response);
        }
        finally {
            afterRequest(request, response);
            response.copyBodyToResponse();
        }
    }

    protected void beforeRequest(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response) {
        if (log.isInfoEnabled()) {
            val address = request.getRemoteAddr();
            val queryString = request.getQueryString();
            if (queryString == null) {
                log.info("{}> {} {}", address, request.getMethod(), request.getRequestURI());
            } else {
                log.info("{}> {} {}?{}", address, request.getMethod(), request.getRequestURI(), queryString);
            }
            Collections.list(request.getHeaderNames()).forEach(headerName ->
                Collections.list(request.getHeaders(headerName)).forEach(headerValue ->
                    log.info("{}> {}: {}", address, headerName, headerValue)));
            val content = request.getContentAsByteArray();
            if (content.length > 0) {
                log.info("{}>", address);
                try {
                    val contentString = new String(content, request.getCharacterEncoding());
                    Stream.of(contentString.split("\r\n|\r|\n")).forEach(line -> log.info("{}> {}", address, line));
                } catch (UnsupportedEncodingException e) {
                    log.info("{}> [{} bytes body]", address, content.length);
                }
            }
            log.info("{}>", address);
        }
    }

    protected void afterRequest(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response) {
        if (log.isInfoEnabled()) {
            val address = request.getRemoteAddr();
            val status = response.getStatus();
            log.info("{}< {} {}", address, status, HttpStatus.valueOf(status).getReasonPhrase());
            response.getHeaderNames().forEach(headerName ->
                response.getHeaders(headerName).forEach(headerValue ->
                    log.info("{}< {}: {}", address, headerName, headerValue)));
            val content = response.getContentAsByteArray();
            if (content.length > 0) {
                log.info("{}<", address);
                try {
                    val contentString = new String(content, request.getCharacterEncoding());
                    Stream.of(contentString.split("\r\n|\r|\n")).forEach(line -> log.info("{}< {}", address, line));
                } catch (UnsupportedEncodingException e) {
                    log.info("{}< [{} bytes body]", address, content.length);
                }
            }
        }
    }

    private static ContentCachingRequestWrapper wrapRequest(HttpServletRequest request) {
        if (request instanceof ContentCachingRequestWrapper) {
            return (ContentCachingRequestWrapper) request;
        } else {
            return new ContentCachingRequestWrapper(request);
        }
    }

    private static ContentCachingResponseWrapper wrapResponse(HttpServletResponse response) {
        if (response instanceof ContentCachingResponseWrapper) {
            return (ContentCachingResponseWrapper) response;
        } else {
            return new ContentCachingResponseWrapper(response);
        }
    }
}

0 个答案:

没有答案