diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java index 1cbb2eb1..f19e3665 100644 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java @@ -125,7 +125,7 @@ public class LogAspect { return false; } // 如果接口匹配排除列表,不记录日志 - if (logProperties.isMatch(attributes.getRequest().getRequestURI())) { + if (logProperties.isMatchExcludeUri(attributes.getRequest().getRequestURI())) { return false; } return logHandler.isRecord(targetMethod, targetClass); diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java index e1648ad9..63389028 100644 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java @@ -64,7 +64,6 @@ public class LogAutoConfiguration { * 日志过滤器 */ @Bean - @ConditionalOnMissingBean public FilterRegistrationBean logFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new LogFilter(logProperties)); diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java index f14e812f..0467f2f3 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java @@ -51,40 +51,41 @@ public class LogFilter extends OncePerRequestFilter { protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException { - if (!this.isFilter(request)) { + if (this.isNotFilter(request)) { filterChain.doFilter(request, response); return; } // 包装可重复读取请求及响应 - boolean isExcludeUri = logProperties.isMatch(request.getRequestURI()); - HttpServletRequest requestWrapper = (isExcludeUri || !this.isRequestWrapper(request)) - ? request + RepeatReadRequestWrapper wrappedRequest = request instanceof RepeatReadRequestWrapper wrapped + ? wrapped : new RepeatReadRequestWrapper(request); - HttpServletResponse responseWrapper = (isExcludeUri || !this.isResponseWrapper(response)) - ? response + RepeatReadResponseWrapper wrappedResponse = response instanceof RepeatReadResponseWrapper wrapped + ? wrapped : new RepeatReadResponseWrapper(response); - filterChain.doFilter(requestWrapper, responseWrapper); + filterChain.doFilter(wrappedRequest, wrappedResponse); - // 如果响应被包装了,复制缓存数据到原始响应 - if (responseWrapper instanceof RepeatReadResponseWrapper wrappedResponse) { - wrappedResponse.copyBodyToResponse(); - } + // 复制缓存数据到原始响应 + wrappedResponse.copyBodyToResponse(); } /** - * 是否过滤请求 + * 是否不过滤 * * @param request 请求对象 - * @return 是否过滤请求 + * @return true: 不过滤; false: 过滤 */ - private boolean isFilter(HttpServletRequest request) { + private boolean isNotFilter(HttpServletRequest request) { if (!isRequestValid(request)) { - return false; + return true; } // 不拦截 /error ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class); - return !request.getRequestURI().equals(serverProperties.getError().getPath()); + if (request.getRequestURI().equals(serverProperties.getError().getPath())) { + return true; + } + // 放行路径 + return logProperties.isMatchExcludeUri(request.getRequestURI()); } /** @@ -101,24 +102,4 @@ public class LogFilter extends OncePerRequestFilter { return false; } } - - /** - * 是否需要包装输入流 - * - * @param request 请求对象 - * @return true:是;false:否 - */ - private boolean isRequestWrapper(HttpServletRequest request) { - return !(request instanceof RepeatReadRequestWrapper); - } - - /** - * 是否需要包装输出流 - * - * @param response 响应对象 - * @return true:是;false:否 - */ - private boolean isResponseWrapper(HttpServletResponse response) { - return !(response instanceof RepeatReadResponseWrapper); - } } diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java index 2b4b1ec5..45bddf8c 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java @@ -21,6 +21,7 @@ import cn.hutool.extra.servlet.JakartaServletUtil; import cn.hutool.json.JSONUtil; import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.util.UriUtils; +import org.springframework.web.util.WebUtils; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.wrapper.RepeatReadRequestWrapper; import top.continew.starter.log.http.RecordableHttpRequest; @@ -79,11 +80,17 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest @Override public String getBody() { - if (request instanceof RepeatReadRequestWrapper wrapper && !wrapper.isMultipartContent(request)) { - String body = JakartaServletUtil.getBody(request); + try { + RepeatReadRequestWrapper wrappedRequest = WebUtils + .getNativeRequest(request, RepeatReadRequestWrapper.class); + if (wrappedRequest == null || wrappedRequest.isMultipartContent(request)) { + return null; + } + String body = wrappedRequest.getContentAsString(); return JSONUtil.isTypeJSON(body) ? body : null; + } catch (Exception e) { + return null; } - return null; } @Override diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpResponse.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpResponse.java index 66ad8b85..174202e1 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpResponse.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpResponse.java @@ -18,6 +18,7 @@ package top.continew.starter.log.http.servlet; import cn.hutool.json.JSONUtil; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.web.util.WebUtils; import top.continew.starter.core.util.ServletUtils; import top.continew.starter.core.wrapper.RepeatReadResponseWrapper; import top.continew.starter.log.http.RecordableHttpResponse; @@ -54,11 +55,17 @@ public final class RecordableServletHttpResponse implements RecordableHttpRespon @Override public String getBody() { - if (response instanceof RepeatReadResponseWrapper wrapper && !wrapper.isStreamingResponse()) { - String body = wrapper.getResponseContent(); + try { + RepeatReadResponseWrapper wrappedResponse = WebUtils + .getNativeResponse(response, RepeatReadResponseWrapper.class); + if (wrappedResponse == null || wrappedResponse.isStreamingResponse()) { + return null; + } + String body = wrappedResponse.getResponseContent(); return JSONUtil.isTypeJSON(body) ? body : null; + } catch (Exception e) { + return null; } - return null; } @Override diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java index 1d344c33..1f6c26c6 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java @@ -92,9 +92,9 @@ public class LogProperties { * 是否匹配放行路由 * * @param uri 请求 URI - * @return 是否匹配 + * @return true: 匹配; false: 不匹配 */ - public boolean isMatch(String uri) { + public boolean isMatchExcludeUri(String uri) { return this.getExcludePatterns().stream().anyMatch(pattern -> SpringWebUtils.isMatch(uri, pattern)); } } diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java index 5686933b..4bea73e6 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java @@ -96,7 +96,7 @@ public class AccessLogUtils { */ public static boolean exclusionPath(LogProperties properties, String path) { // 放行路由配置的排除检查 - return properties.isMatch(path) || RESOURCE_PATH.stream() + return properties.isMatchExcludeUri(path) || RESOURCE_PATH.stream() .anyMatch(resourcePath -> SpringWebUtils.isMatchAnt(path, resourcePath)); } diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java index 854779c8..0df89532 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java @@ -70,7 +70,6 @@ public class LogAutoConfiguration implements WebMvcConfigurer { * 日志过滤器 */ @Bean - @ConditionalOnMissingBean public FilterRegistrationBean logFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new LogFilter(logProperties));