From bd07f9b41f5eabe380c91c18877886fa97e1bc20 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 31 Jul 2024 21:11:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(log):=20=E6=96=B0=E5=A2=9E=20excludePa?= =?UTF-8?q?tterns=20=E6=94=BE=E8=A1=8C=E8=B7=AF=E7=94=B1=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autoconfigure/LogProperties.java | 15 +++++++++ .../log/httptracepro/handler/LogFilter.java | 33 ++++++++++++++++--- .../httptracepro/handler/LogInterceptor.java | 12 ++----- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/autoconfigure/LogProperties.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/autoconfigure/LogProperties.java index 8c734559..f52a2a4a 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/autoconfigure/LogProperties.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/autoconfigure/LogProperties.java @@ -20,7 +20,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.log.core.enums.Include; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -47,6 +49,11 @@ public class LogProperties { */ private Set includes = new HashSet<>(Include.defaultIncludes()); + /** + * 放行路由 + */ + private List excludePatterns = new ArrayList<>(); + public boolean isEnabled() { return enabled; } @@ -70,4 +77,12 @@ public class LogProperties { public void setIncludes(Set includes) { this.includes = includes; } + + public List getExcludePatterns() { + return excludePatterns; + } + + public void setExcludePatterns(List excludePatterns) { + this.excludePatterns = excludePatterns; + } } diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogFilter.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogFilter.java index e565f070..41aebaea 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogFilter.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogFilter.java @@ -16,10 +16,12 @@ package top.continew.starter.log.httptracepro.handler; +import cn.hutool.extra.spring.SpringUtil; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.core.Ordered; import org.springframework.lang.NonNull; import org.springframework.web.filter.OncePerRequestFilter; @@ -28,6 +30,7 @@ import org.springframework.web.util.ContentCachingResponseWrapper; import org.springframework.web.util.WebUtils; import top.continew.starter.log.core.enums.Include; import top.continew.starter.log.httptracepro.autoconfigure.LogProperties; +import top.continew.starter.web.util.SpringWebUtils; import java.io.IOException; import java.net.URI; @@ -63,26 +66,48 @@ public class LogFilter extends OncePerRequestFilter implements Ordered { protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException { - if (!isRequestValid(request)) { + if (!this.isFilter(request)) { filterChain.doFilter(request, response); return; } // 包装输入流,可重复读取 - if (isRequestWrapper(request)) { + if (this.isRequestWrapper(request)) { request = new ContentCachingRequestWrapper(request); } // 包装输出流,可重复读取 - boolean isResponseWrapper = isResponseWrapper(response); + boolean isResponseWrapper = this.isResponseWrapper(response); if (isResponseWrapper) { response = new ContentCachingResponseWrapper(response); } filterChain.doFilter(request, response); // 更新响应(不操作这一步,会导致接口响应空白) if (isResponseWrapper) { - updateResponse(response); + this.updateResponse(response); } } + /** + * 是否过滤请求 + * + * @param request 请求对象 + * @return 是否过滤请求 + */ + private boolean isFilter(HttpServletRequest request) { + if (!isRequestValid(request)) { + return false; + } + // 不拦截 /error + ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class); + if (request.getRequestURI().equals(serverProperties.getError().getPath())) { + return false; + } + // 放行 + boolean isMatch = logProperties.getExcludePatterns() + .stream() + .anyMatch(pattern -> SpringWebUtils.match(pattern, request.getRequestURI())); + return !isMatch; + } + /** * 请求是否有效 * diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogInterceptor.java index a28f94dc..6d8def3b 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/continew/starter/log/httptracepro/handler/LogInterceptor.java @@ -17,7 +17,6 @@ package top.continew.starter.log.httptracepro.handler; import cn.hutool.core.text.CharSequenceUtil; -import cn.hutool.extra.spring.SpringUtil; import com.alibaba.ttl.TransmittableThreadLocal; import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; @@ -26,7 +25,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.lang.NonNull; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; @@ -63,7 +61,7 @@ public class LogInterceptor implements HandlerInterceptor { @NonNull HttpServletResponse response, @NonNull Object handler) { Clock timestamp = Clock.systemUTC(); - if (this.isRequestRecord(handler, request)) { + if (this.isRequestRecord(handler)) { if (Boolean.TRUE.equals(logProperties.getIsPrint())) { log.info("[{}] {}", request.getMethod(), request.getRequestURI()); } @@ -194,18 +192,12 @@ public class LogInterceptor implements HandlerInterceptor { * 是否要记录日志 * * @param handler 处理器 - * @param request 请求对象 * @return true:需要记录;false:不需要记录 */ - private boolean isRequestRecord(Object handler, HttpServletRequest request) { + private boolean isRequestRecord(Object handler) { if (!(handler instanceof HandlerMethod handlerMethod)) { return false; } - // 不拦截 /error - ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class); - if (request.getRequestURI().equals(serverProperties.getError().getPath())) { - return false; - } // 如果接口被隐藏,不记录日志 Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class); if (null != methodOperation && methodOperation.hidden()) {