Skip to content

Commit 58e234a

Browse files
committed
perf(log): 扩展静态资源路径匹配规则
- 新增 AntPathMatcher 支持更灵活的路径匹配 - 更新 RESOURCE_PATH 列表以包含更多静态资源路径模式 - 实现 isMatchAnt 方法用于 Ant 风格路径匹配 - 添加多个新的静态资源排除路径,如 /actuator/**、/favicon.ico 等 - 修改访问日志工具类使用新的 Ant 路径匹配方法
1 parent f1937d3 commit 58e234a

File tree

2 files changed

+30
-3
lines changed
  • continew-starter-core/src/main/java/top/continew/starter/core/util
  • continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util

2 files changed

+30
-3
lines changed

continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import jakarta.servlet.http.HttpServletRequest;
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.http.server.PathContainer;
26+
import org.springframework.util.AntPathMatcher;
2627
import org.springframework.web.accept.ContentNegotiationManager;
2728
import org.springframework.web.method.HandlerMethod;
2829
import org.springframework.web.servlet.HandlerExecutionChain;
@@ -50,6 +51,8 @@ public class SpringWebUtils {
5051
private SpringWebUtils() {
5152
}
5253

54+
private static final AntPathMatcher matcher = new AntPathMatcher();
55+
5356
/**
5457
* 路径是否匹配
5558
*
@@ -88,6 +91,30 @@ public static boolean isMatch(String path, String pattern) {
8891
return pathPattern.matches(pathContainer);
8992
}
9093

94+
/**
95+
* 路径是否匹配 - Ant 风格
96+
*
97+
* @param path 路径
98+
* @param pattern 匹配模式
99+
* @return 是否匹配
100+
* @since 2.4.0
101+
*/
102+
public static boolean isMatchAnt(String path, String pattern) {
103+
return matcher.match(pattern, path);
104+
}
105+
106+
/**
107+
* 路径是否匹配 - Ant 风格
108+
*
109+
* @param path 路径
110+
* @param patterns 匹配模式列表
111+
* @return 是否匹配
112+
* @since 2.6.0
113+
*/
114+
public static boolean isMatchAnt(String path, List<String> patterns) {
115+
return patterns.stream().anyMatch(pattern -> isMatchAnt(path, pattern));
116+
}
117+
91118
/**
92119
* 取消注册静态资源映射
93120
*

continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ private AccessLogUtils() {
4040
}
4141

4242
/**
43-
* 资源路径 - doc 路径
43+
* 静态资源路径模式
4444
*/
4545
private static final List<String> RESOURCE_PATH = List
46-
.of("/doc/**", "/v2/api-docs/**", "/v3/api-docs/**", "/webjars/**", "/swagger-resources/**", "/swagger-ui.html");
46+
.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");
4747

4848
/**
4949
* 获取参数信息
@@ -91,7 +91,7 @@ public static String getParam(AccessLogProperties properties) {
9191
public static boolean exclusionPath(LogProperties properties, String path) {
9292
// 放行路由配置的排除检查
9393
return properties.isMatch(path) || RESOURCE_PATH.stream()
94-
.anyMatch(resourcePath -> SpringWebUtils.isMatch(path, resourcePath));
94+
.anyMatch(resourcePath -> SpringWebUtils.isMatchAnt(path, resourcePath));
9595
}
9696

9797
/**

0 commit comments

Comments
 (0)