From 58e234a929d5cbc826b4a9251b074bb82862eaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=B3=BD=E5=A8=81?= <958142070@qq.com> Date: Mon, 20 Oct 2025 16:07:31 +0800 Subject: [PATCH] =?UTF-8?q?perf(log):=20=E6=89=A9=E5=B1=95=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B5=84=E6=BA=90=E8=B7=AF=E5=BE=84=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 AntPathMatcher 支持更灵活的路径匹配 - 更新 RESOURCE_PATH 列表以包含更多静态资源路径模式 - 实现 isMatchAnt 方法用于 Ant 风格路径匹配 - 添加多个新的静态资源排除路径,如 /actuator/**、/favicon.ico 等 - 修改访问日志工具类使用新的 Ant 路径匹配方法 --- .../starter/core/util/SpringWebUtils.java | 27 +++++++++++++++++++ .../starter/log/util/AccessLogUtils.java | 6 ++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java index 031d54a9..9743a0a6 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java @@ -23,6 +23,7 @@ import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import org.springframework.context.ApplicationContext; import org.springframework.http.server.PathContainer; +import org.springframework.util.AntPathMatcher; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerExecutionChain; @@ -50,6 +51,8 @@ public class SpringWebUtils { private SpringWebUtils() { } + private static final AntPathMatcher matcher = new AntPathMatcher(); + /** * 路径是否匹配 * @@ -88,6 +91,30 @@ public class SpringWebUtils { return pathPattern.matches(pathContainer); } + /** + * 路径是否匹配 - Ant 风格 + * + * @param path 路径 + * @param pattern 匹配模式 + * @return 是否匹配 + * @since 2.4.0 + */ + public static boolean isMatchAnt(String path, String pattern) { + return matcher.match(pattern, path); + } + + /** + * 路径是否匹配 - Ant 风格 + * + * @param path 路径 + * @param patterns 匹配模式列表 + * @return 是否匹配 + * @since 2.6.0 + */ + public static boolean isMatchAnt(String path, List patterns) { + return patterns.stream().anyMatch(pattern -> isMatchAnt(path, 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 9db8f446..8fb221ed 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 @@ -40,10 +40,10 @@ public class AccessLogUtils { } /** - * 资源路径 - doc 路径 + * 静态资源路径模式 */ private static final List RESOURCE_PATH = List - .of("/doc/**", "/v2/api-docs/**", "/v3/api-docs/**", "/webjars/**", "/swagger-resources/**", "/swagger-ui.html"); + .of("/**/doc/**", "/**/doc.html", "/**/nextdoc/**", "/**/v*/api-docs/**", "/**/api-docs/**", "/**/swagger-ui/**", "/**/swagger-ui.html", "/**/swagger-resources/**", "/**/webjars/**", "/**/favicon.ico", "/**/static/**", "/**/assets/**", "/**/actuator/**", "/error", "/health"); /** * 获取参数信息 @@ -91,7 +91,7 @@ public class AccessLogUtils { public static boolean exclusionPath(LogProperties properties, String path) { // 放行路由配置的排除检查 return properties.isMatch(path) || RESOURCE_PATH.stream() - .anyMatch(resourcePath -> SpringWebUtils.isMatch(path, resourcePath)); + .anyMatch(resourcePath -> SpringWebUtils.isMatchAnt(path, resourcePath)); } /**