From e55eb17d64d7b42c6e64b18a645cda9558f08d58 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 19 Aug 2024 22:40:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E6=96=B0=E5=A2=9E=20isMatch=20?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=98=AF=E5=90=A6=E5=8C=B9=E9=85=8D=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit match => isMatch --- .../log/interceptor/handler/LogFilter.java | 2 +- .../web/autoconfigure/xss/XssFilter.java | 27 +++------------- .../starter/web/util/SpringWebUtils.java | 32 +++++++++++++++++-- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogFilter.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogFilter.java index acc0a4e2..b85ef9f5 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogFilter.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogFilter.java @@ -104,7 +104,7 @@ private boolean isFilter(HttpServletRequest request) { // 放行 boolean isMatch = logProperties.getExcludePatterns() .stream() - .anyMatch(pattern -> SpringWebUtils.match(pattern, request.getRequestURI())); + .anyMatch(pattern -> SpringWebUtils.isMatch(pattern, request.getRequestURI())); return !isMatch; } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/xss/XssFilter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/xss/XssFilter.java index 642ef9eb..c79fef45 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/xss/XssFilter.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/xss/XssFilter.java @@ -21,9 +21,7 @@ import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.server.PathContainer; -import org.springframework.web.util.pattern.PathPattern; -import org.springframework.web.util.pattern.PathPatternParser; +import top.continew.starter.web.util.SpringWebUtils; import java.io.IOException; import java.util.List; @@ -57,14 +55,15 @@ public void doFilter(ServletRequest servletRequest, if (servletRequest instanceof HttpServletRequest request && xssProperties.isEnabled()) { // 放行路由:忽略 XSS 过滤 List excludePatterns = xssProperties.getExcludePatterns(); - if (CollectionUtil.isNotEmpty(excludePatterns) && isMatchPath(request.getServletPath(), excludePatterns)) { + if (CollectionUtil.isNotEmpty(excludePatterns) && SpringWebUtils.isMatch(request + .getServletPath(), excludePatterns)) { filterChain.doFilter(request, servletResponse); return; } // 拦截路由:执行 XSS 过滤 List includePatterns = xssProperties.getIncludePatterns(); if (CollectionUtil.isNotEmpty(includePatterns)) { - if (isMatchPath(request.getServletPath(), includePatterns)) { + if (SpringWebUtils.isMatch(request.getServletPath(), includePatterns)) { filterChain.doFilter(new XssServletRequestWrapper(request, xssProperties), servletResponse); } else { filterChain.doFilter(request, servletResponse); @@ -77,22 +76,4 @@ public void doFilter(ServletRequest servletRequest, } filterChain.doFilter(servletRequest, servletResponse); } - - /** - * 判断数组中是否存在匹配的路径 - * - * @param requestUrl 请求地址 - * @param pathPatterns 指定匹配路径 - * @return true:匹配;false:不匹配 - */ - private static boolean isMatchPath(String requestUrl, List pathPatterns) { - for (String pattern : pathPatterns) { - PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern); - PathContainer pathContainer = PathContainer.parsePath(requestUrl); - if (pathPattern.matches(pathContainer)) { - return true; - } - } - return false; - } } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java index f8d689e0..adaffea8 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java @@ -16,8 +16,8 @@ package top.continew.starter.web.util; -import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.text.CharSequenceUtil; +import cn.hutool.core.util.ReflectUtil; import cn.hutool.extra.spring.SpringUtil; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; @@ -35,6 +35,8 @@ import org.springframework.web.util.pattern.PathPatternParser; import top.continew.starter.core.constant.StringConstants; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -70,12 +72,36 @@ public static HttpServletResponse getResponse() { /** * 路径是否匹配 * - * @param pattern 匹配模式 + * @param path 路径 + * @param patterns 匹配模式列表 + * @return 是否匹配 + * @since 2.6.0 + */ + public static boolean isMatch(String path, List patterns) { + return patterns.stream().anyMatch(pattern -> isMatch(path, pattern)); + } + + /** + * 路径是否匹配 + * + * @param path 路径 + * @param patterns 匹配模式列表 + * @return 是否匹配 + * @since 2.6.0 + */ + public static boolean isMatch(String path, String... patterns) { + return Arrays.stream(patterns).anyMatch(pattern -> isMatch(path, pattern)); + } + + /** + * 路径是否匹配 + * * @param path 路径 + * @param pattern 匹配模式 * @return 是否匹配 * @since 2.4.0 */ - public static boolean match(String pattern, String path) { + public static boolean isMatch(String path, String pattern) { PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern); PathContainer pathContainer = PathContainer.parsePath(path); return pathPattern.matches(pathContainer);