From 702dcca7012a4ac3d779396f12ef9eeb8371f7cb Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 31 Jul 2024 21:10:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20SpringWebUtils=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20match=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ServletUtils 部分方法调整到 SpringWebUtils --- .../limiter/core/RateLimiterAspect.java | 4 +- .../starter/web/util/ServletUtils.java | 24 ---------- .../starter/web/util/SpringWebUtils.java | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/continew-starter-security/continew-starter-security-limiter/src/main/java/top/continew/starter/security/limiter/core/RateLimiterAspect.java b/continew-starter-security/continew-starter-security-limiter/src/main/java/top/continew/starter/security/limiter/core/RateLimiterAspect.java index 007b7086..4ad5f035 100644 --- a/continew-starter-security/continew-starter-security-limiter/src/main/java/top/continew/starter/security/limiter/core/RateLimiterAspect.java +++ b/continew-starter-security/continew-starter-security-limiter/src/main/java/top/continew/starter/security/limiter/core/RateLimiterAspect.java @@ -36,7 +36,7 @@ import top.continew.starter.security.limiter.annotation.RateLimiters; import top.continew.starter.security.limiter.autoconfigure.RateLimiterProperties; import top.continew.starter.security.limiter.enums.LimitType; import top.continew.starter.security.limiter.exception.RateLimiterException; -import top.continew.starter.web.util.ServletUtils; +import top.continew.starter.web.util.SpringWebUtils; import java.lang.reflect.Method; import java.util.Objects; @@ -171,7 +171,7 @@ public class RateLimiterAspect { } // 获取后缀 String suffix = switch (rateLimiter.type()) { - case IP -> JakartaServletUtil.getClientIP(ServletUtils.getRequest()); + case IP -> JakartaServletUtil.getClientIP(SpringWebUtils.getRequest()); case CLUSTER -> redissonClient.getId(); default -> StringConstants.EMPTY; }; diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java index 9d302c21..a607b866 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java @@ -21,8 +21,6 @@ import cn.hutool.http.useragent.UserAgent; import cn.hutool.http.useragent.UserAgentUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import top.continew.starter.core.constant.StringConstants; import java.util.*; @@ -38,24 +36,6 @@ public class ServletUtils { private ServletUtils() { } - /** - * 获取请求对象 - * - * @return / - */ - public static HttpServletRequest getRequest() { - return getServletRequestAttributes().getRequest(); - } - - /** - * 获取响应对象 - * - * @return / - */ - public static HttpServletResponse getResponse() { - return getServletRequestAttributes().getResponse(); - } - /** * 获取浏览器及其版本信息 * @@ -118,8 +98,4 @@ public class ServletUtils { } return headerMap; } - - private static ServletRequestAttributes getServletRequestAttributes() { - return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes()); - } } 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 4ce6689c..f8d689e0 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 @@ -20,15 +20,23 @@ import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.extra.spring.SpringUtil; import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; +import org.springframework.http.server.PathContainer; import org.springframework.web.accept.ContentNegotiationManager; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.util.UrlPathHelper; +import org.springframework.web.util.pattern.PathPattern; +import org.springframework.web.util.pattern.PathPatternParser; import top.continew.starter.core.constant.StringConstants; import java.util.Map; +import java.util.Objects; /** * Spring Web 工具类 @@ -41,6 +49,38 @@ public class SpringWebUtils { private SpringWebUtils() { } + /** + * 获取请求对象 + * + * @return 请求对象 + */ + public static HttpServletRequest getRequest() { + return getServletRequestAttributes().getRequest(); + } + + /** + * 获取响应对象 + * + * @return 响应对象 + */ + public static HttpServletResponse getResponse() { + return getServletRequestAttributes().getResponse(); + } + + /** + * 路径是否匹配 + * + * @param pattern 匹配模式 + * @param path 路径 + * @return 是否匹配 + * @since 2.4.0 + */ + public static boolean match(String pattern, String path) { + PathPattern pathPattern = PathPatternParser.defaultInstance.parse(pattern); + PathContainer pathContainer = PathContainer.parsePath(path); + return pathPattern.matches(pathContainer); + } + /** * 取消注册静态资源映射 * @@ -91,4 +131,8 @@ public class SpringWebUtils { .getUrlMap(); ReflectUtil.invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap); } + + private static ServletRequestAttributes getServletRequestAttributes() { + return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes()); + } }