From 45458ddb6b5a36176b2fcdc833984ad7aefc97d3 Mon Sep 17 00:00:00 2001 From: ruying408 <1877972603@qq.com> Date: Wed, 16 Oct 2024 23:53:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20token=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=99=A8=20TokenIgnore=20=E6=B3=A8=E8=A7=A3=E5=9C=A8=E7=B1=BB?= =?UTF-8?q?=E4=B8=8A=E6=8C=87=E5=AE=9A=E6=96=B9=E6=B3=95=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=95=B4=E4=B8=AA=E7=B1=BB=E6=8E=A5=E5=8F=A3=E9=83=BD=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JwtAuthenticationTokenFilter.java | 28 +++++-------------- .../cool/core/security/JwtSecurityConfig.java | 4 +++ .../java/com/cool/core/util/PathUtils.java | 16 +++++++++++ .../sys/impl/BaseSysLogServiceImpl.java | 7 ++--- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/cool/core/security/JwtAuthenticationTokenFilter.java b/src/main/java/com/cool/core/security/JwtAuthenticationTokenFilter.java index 91ca984..05209df 100644 --- a/src/main/java/com/cool/core/security/JwtAuthenticationTokenFilter.java +++ b/src/main/java/com/cool/core/security/JwtAuthenticationTokenFilter.java @@ -4,11 +4,11 @@ import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.jwt.JWT; -import com.cool.core.annotation.TokenIgnore; import com.cool.core.cache.CoolCache; import com.cool.core.enums.UserTypeEnum; import com.cool.core.security.jwt.JwtTokenUtil; import com.cool.core.security.jwt.JwtUser; +import com.cool.core.util.PathUtils; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; @@ -23,9 +23,6 @@ import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.method.HandlerMethod; -import org.springframework.web.servlet.HandlerExecutionChain; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; /** * Token过滤器 @@ -37,28 +34,17 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { final private JwtTokenUtil jwtTokenUtil; final private CoolCache coolCache; - private final RequestMappingHandlerMapping requestMappingHandlerMapping; - + final private IgnoredUrlsProperties ignoredUrlsProperties; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { - try { - HandlerExecutionChain handlerExecutionChain = requestMappingHandlerMapping.getHandler(request); - if (handlerExecutionChain != null) { - Object handler = handlerExecutionChain.getHandler(); - if (handler instanceof HandlerMethod) { - HandlerMethod handlerMethod = (HandlerMethod) handler; - if (handlerMethod.getMethodAnnotation(TokenIgnore.class) != null || - handlerMethod.getBeanType().getAnnotation(TokenIgnore.class) != null) { - chain.doFilter(request, response); - return; - } - } - } - } catch (Exception e) { - throw new RuntimeException(e); + String requestURI = request.getRequestURI(); + if (PathUtils.isMatch(ignoredUrlsProperties.getAdminAuthUrls(), requestURI)) { + // 请求路径在忽略后台鉴权url里支持通配符,放行 + chain.doFilter(request, response); + return; } String authToken = request.getHeader("Authorization"); if (!StrUtil.isEmpty(authToken)) { diff --git a/src/main/java/com/cool/core/security/JwtSecurityConfig.java b/src/main/java/com/cool/core/security/JwtSecurityConfig.java index fc28b65..6f9e722 100644 --- a/src/main/java/com/cool/core/security/JwtSecurityConfig.java +++ b/src/main/java/com/cool/core/security/JwtSecurityConfig.java @@ -94,6 +94,10 @@ public class JwtSecurityConfig { for (String path : tokenIgnoreCtr.value()) { ignoredUrlsProperties.getAdminAuthUrls().add(String.join("/", urls) + "/" + path); } + if (tokenIgnoreCtr.value().length == 0) { + // 通配 + ignoredUrlsProperties.getAdminAuthUrls().add(String.join("/", urls)+ "/**"); + } handlerCtr.add(handlerMethod.getBeanType().getName()); }); } diff --git a/src/main/java/com/cool/core/util/PathUtils.java b/src/main/java/com/cool/core/util/PathUtils.java index f384e74..0f61184 100644 --- a/src/main/java/com/cool/core/util/PathUtils.java +++ b/src/main/java/com/cool/core/util/PathUtils.java @@ -1,12 +1,15 @@ package com.cool.core.util; import cn.hutool.core.io.file.PathUtil; +import cn.hutool.core.text.AntPathMatcher; import com.cool.CoolApplication; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; public class PathUtils { + private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); public static boolean isAbsolutePath(String pathStr) { Path path = Paths.get(pathStr); @@ -51,4 +54,17 @@ public class PathUtils { PathUtil.mkParentDirs(path); } } + + /** + * 判断给定的请求URI是否匹配列表中的任意一个URL模式 + * 使用Ant风格的路径匹配来处理URL模式,提供了一种通配符匹配的方法 + * + * @param urls 待匹配的URL模式列表 + * @param requestURI 请求的URI + * @return 如果请求URI匹配列表中的任意一个URL模式,则返回true;否则返回false + */ + public static boolean isMatch(List urls, String requestURI) { + return urls.stream() + .anyMatch(url -> antPathMatcher.match(url, requestURI)); + } } diff --git a/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java b/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java index 877969d..fd4886c 100644 --- a/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java +++ b/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java @@ -1,7 +1,6 @@ package com.cool.modules.base.service.sys.impl; import cn.hutool.core.date.DateUtil; -import cn.hutool.core.text.AntPathMatcher; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; @@ -10,6 +9,7 @@ import com.cool.core.config.LogProperties; import com.cool.core.security.IgnoredUrlsProperties; import com.cool.core.util.CoolSecurityUtil; import com.cool.core.util.IPUtils; +import com.cool.core.util.PathUtils; import com.cool.modules.base.entity.sys.BaseSysLogEntity; import com.cool.modules.base.entity.sys.BaseSysUserEntity; import com.cool.modules.base.entity.sys.table.BaseSysLogEntityTableDef; @@ -43,8 +43,6 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl page, QueryWrapper queryWrapper) { @@ -83,8 +81,7 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl antPathMatcher.match(url, requestURI)); + return PathUtils.isMatch(ignoredUrlsProperties.getLogUrls(), requestURI); } public void recordAsync(String ipAddr, String requestURI, JSONObject requestParams) {