mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-10-21 20:57:15 +08:00
perf(log): 扩展静态资源路径匹配规则
- 新增 AntPathMatcher 支持更灵活的路径匹配 - 更新 RESOURCE_PATH 列表以包含更多静态资源路径模式 - 实现 isMatchAnt 方法用于 Ant 风格路径匹配 - 添加多个新的静态资源排除路径,如 /actuator/**、/favicon.ico 等 - 修改访问日志工具类使用新的 Ant 路径匹配方法
This commit is contained in:
@@ -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<String> patterns) {
|
||||
return patterns.stream().anyMatch(pattern -> isMatchAnt(path, pattern));
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消注册静态资源映射
|
||||
*
|
||||
|
@@ -40,10 +40,10 @@ public class AccessLogUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源路径 - doc 路径
|
||||
* 静态资源路径模式
|
||||
*/
|
||||
private static final List<String> 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user