mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-10 20:57:18 +08:00
feat(web): SpringWebUtils 新增 match 方法
ServletUtils 部分方法调整到 SpringWebUtils
This commit is contained in:
@@ -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.autoconfigure.RateLimiterProperties;
|
||||||
import top.continew.starter.security.limiter.enums.LimitType;
|
import top.continew.starter.security.limiter.enums.LimitType;
|
||||||
import top.continew.starter.security.limiter.exception.RateLimiterException;
|
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.lang.reflect.Method;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -171,7 +171,7 @@ public class RateLimiterAspect {
|
|||||||
}
|
}
|
||||||
// 获取后缀
|
// 获取后缀
|
||||||
String suffix = switch (rateLimiter.type()) {
|
String suffix = switch (rateLimiter.type()) {
|
||||||
case IP -> JakartaServletUtil.getClientIP(ServletUtils.getRequest());
|
case IP -> JakartaServletUtil.getClientIP(SpringWebUtils.getRequest());
|
||||||
case CLUSTER -> redissonClient.getId();
|
case CLUSTER -> redissonClient.getId();
|
||||||
default -> StringConstants.EMPTY;
|
default -> StringConstants.EMPTY;
|
||||||
};
|
};
|
||||||
|
@@ -21,8 +21,6 @@ import cn.hutool.http.useragent.UserAgent;
|
|||||||
import cn.hutool.http.useragent.UserAgentUtil;
|
import cn.hutool.http.useragent.UserAgentUtil;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
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 top.continew.starter.core.constant.StringConstants;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -38,24 +36,6 @@ public class ServletUtils {
|
|||||||
private 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;
|
return headerMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ServletRequestAttributes getServletRequestAttributes() {
|
|
||||||
return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -20,15 +20,23 @@ import cn.hutool.core.util.ReflectUtil;
|
|||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
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.HttpServletResponse;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.http.server.PathContainer;
|
||||||
import org.springframework.web.accept.ContentNegotiationManager;
|
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.HandlerMapping;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
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 top.continew.starter.core.constant.StringConstants;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Web 工具类
|
* Spring Web 工具类
|
||||||
@@ -41,6 +49,38 @@ public class SpringWebUtils {
|
|||||||
private 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();
|
.getUrlMap();
|
||||||
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
|
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ServletRequestAttributes getServletRequestAttributes() {
|
||||||
|
return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user