mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-11-10 14:57:10 +08:00
feat(web): 新增 isMatch 路径是否匹配方法
match => isMatch
This commit is contained in:
@@ -104,7 +104,7 @@ public class LogFilter extends OncePerRequestFilter implements Ordered {
|
|||||||
// 放行
|
// 放行
|
||||||
boolean isMatch = logProperties.getExcludePatterns()
|
boolean isMatch = logProperties.getExcludePatterns()
|
||||||
.stream()
|
.stream()
|
||||||
.anyMatch(pattern -> SpringWebUtils.match(pattern, request.getRequestURI()));
|
.anyMatch(pattern -> SpringWebUtils.isMatch(pattern, request.getRequestURI()));
|
||||||
return !isMatch;
|
return !isMatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,9 +21,7 @@ import jakarta.servlet.*;
|
|||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.server.PathContainer;
|
import top.continew.starter.web.util.SpringWebUtils;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
|
||||||
import org.springframework.web.util.pattern.PathPatternParser;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -57,14 +55,15 @@ public class XssFilter implements Filter {
|
|||||||
if (servletRequest instanceof HttpServletRequest request && xssProperties.isEnabled()) {
|
if (servletRequest instanceof HttpServletRequest request && xssProperties.isEnabled()) {
|
||||||
// 放行路由:忽略 XSS 过滤
|
// 放行路由:忽略 XSS 过滤
|
||||||
List<String> excludePatterns = xssProperties.getExcludePatterns();
|
List<String> excludePatterns = xssProperties.getExcludePatterns();
|
||||||
if (CollectionUtil.isNotEmpty(excludePatterns) && isMatchPath(request.getServletPath(), excludePatterns)) {
|
if (CollectionUtil.isNotEmpty(excludePatterns) && SpringWebUtils.isMatch(request
|
||||||
|
.getServletPath(), excludePatterns)) {
|
||||||
filterChain.doFilter(request, servletResponse);
|
filterChain.doFilter(request, servletResponse);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 拦截路由:执行 XSS 过滤
|
// 拦截路由:执行 XSS 过滤
|
||||||
List<String> includePatterns = xssProperties.getIncludePatterns();
|
List<String> includePatterns = xssProperties.getIncludePatterns();
|
||||||
if (CollectionUtil.isNotEmpty(includePatterns)) {
|
if (CollectionUtil.isNotEmpty(includePatterns)) {
|
||||||
if (isMatchPath(request.getServletPath(), includePatterns)) {
|
if (SpringWebUtils.isMatch(request.getServletPath(), includePatterns)) {
|
||||||
filterChain.doFilter(new XssServletRequestWrapper(request, xssProperties), servletResponse);
|
filterChain.doFilter(new XssServletRequestWrapper(request, xssProperties), servletResponse);
|
||||||
} else {
|
} else {
|
||||||
filterChain.doFilter(request, servletResponse);
|
filterChain.doFilter(request, servletResponse);
|
||||||
@@ -77,22 +76,4 @@ public class XssFilter implements Filter {
|
|||||||
}
|
}
|
||||||
filterChain.doFilter(servletRequest, servletResponse);
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断数组中是否存在匹配的路径
|
|
||||||
*
|
|
||||||
* @param requestUrl 请求地址
|
|
||||||
* @param pathPatterns 指定匹配路径
|
|
||||||
* @return true:匹配;false:不匹配
|
|
||||||
*/
|
|
||||||
private static boolean isMatchPath(String requestUrl, List<String> pathPatterns) {
|
|
||||||
for (String pattern : pathPatterns) {
|
|
||||||
PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern);
|
|
||||||
PathContainer pathContainer = PathContainer.parsePath(requestUrl);
|
|
||||||
if (pathPattern.matches(pathContainer)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package top.continew.starter.web.util;
|
package top.continew.starter.web.util;
|
||||||
|
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import jakarta.servlet.ServletContext;
|
import jakarta.servlet.ServletContext;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -35,6 +35,8 @@ import org.springframework.web.util.pattern.PathPattern;
|
|||||||
import org.springframework.web.util.pattern.PathPatternParser;
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
import top.continew.starter.core.constant.StringConstants;
|
import top.continew.starter.core.constant.StringConstants;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@@ -70,12 +72,36 @@ public class SpringWebUtils {
|
|||||||
/**
|
/**
|
||||||
* 路径是否匹配
|
* 路径是否匹配
|
||||||
*
|
*
|
||||||
* @param pattern 匹配模式
|
* @param path 路径
|
||||||
|
* @param patterns 匹配模式列表
|
||||||
|
* @return 是否匹配
|
||||||
|
* @since 2.6.0
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(String path, List<String> 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 path 路径
|
||||||
|
* @param pattern 匹配模式
|
||||||
* @return 是否匹配
|
* @return 是否匹配
|
||||||
* @since 2.4.0
|
* @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);
|
PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern);
|
||||||
PathContainer pathContainer = PathContainer.parsePath(path);
|
PathContainer pathContainer = PathContainer.parsePath(path);
|
||||||
return pathPattern.matches(pathContainer);
|
return pathPattern.matches(pathContainer);
|
||||||
|
|||||||
Reference in New Issue
Block a user