mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 18:22:15 +08:00
新增SaRouterUtil,可优雅的路由拦截式鉴权
This commit is contained in:
@@ -146,7 +146,4 @@ public class SaTokenManager {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,33 @@
|
|||||||
|
package cn.dev33.satoken.fun;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据boolean变量,决定是否执行一个函数
|
||||||
|
* @author kong
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class IsRunFunction {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 变量
|
||||||
|
*/
|
||||||
|
public Boolean isRun;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定一个变量,如果为true,则执行exe函数
|
||||||
|
* @param isRun 变量
|
||||||
|
*/
|
||||||
|
public IsRunFunction(boolean isRun) {
|
||||||
|
this.isRun = isRun;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据变量决定是否执行此函数
|
||||||
|
* @param function 函数
|
||||||
|
*/
|
||||||
|
public void exe(SaFunction function) {
|
||||||
|
if(isRun) {
|
||||||
|
function.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package cn.dev33.satoken.stp;
|
package cn.dev33.satoken.fun;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模拟身份方法的辅助类
|
* 模拟身份方法的辅助类
|
@@ -19,6 +19,7 @@ import cn.dev33.satoken.dao.SaTokenDao;
|
|||||||
import cn.dev33.satoken.exception.NotLoginException;
|
import cn.dev33.satoken.exception.NotLoginException;
|
||||||
import cn.dev33.satoken.exception.NotPermissionException;
|
import cn.dev33.satoken.exception.NotPermissionException;
|
||||||
import cn.dev33.satoken.exception.NotRoleException;
|
import cn.dev33.satoken.exception.NotRoleException;
|
||||||
|
import cn.dev33.satoken.fun.SaFunction;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
import cn.dev33.satoken.session.TokenSign;
|
import cn.dev33.satoken.session.TokenSign;
|
||||||
import cn.dev33.satoken.util.SaTokenConsts;
|
import cn.dev33.satoken.util.SaTokenConsts;
|
||||||
|
@@ -2,6 +2,7 @@ package cn.dev33.satoken.stp;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.fun.SaFunction;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -31,7 +31,7 @@ public class MySaTokenConfig implements WebMvcConfigurer {
|
|||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
// 注册注解拦截器
|
// 注册注解拦截器
|
||||||
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**"); // 全局拦截器
|
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.PathMatcher;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaTokenManager;
|
import cn.dev33.satoken.SaTokenManager;
|
||||||
import cn.dev33.satoken.action.SaTokenAction;
|
import cn.dev33.satoken.action.SaTokenAction;
|
||||||
@@ -97,4 +98,20 @@ public class SaTokenSpringAutowired {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由匹配器
|
||||||
|
*/
|
||||||
|
public static PathMatcher pathMatcher;
|
||||||
|
/**
|
||||||
|
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||||
|
* @param pathMatcher 要设置的 pathMatcher
|
||||||
|
*/
|
||||||
|
@Autowired(required = false)
|
||||||
|
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||||
|
SaTokenSpringAutowired.pathMatcher = pathMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -43,7 +43,8 @@ public class SaRouteInterceptor implements HandlerInterceptor {
|
|||||||
*/
|
*/
|
||||||
private SaRouteFunction function;
|
private SaRouteFunction function;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表示登录验证
|
* 表示登录验证
|
||||||
*/
|
*/
|
||||||
@@ -144,7 +145,6 @@ public class SaRouteInterceptor implements HandlerInterceptor {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ----------------- 构建相关 -----------------
|
// ----------------- 构建相关 -----------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -0,0 +1,163 @@
|
|||||||
|
package cn.dev33.satoken.interceptor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
import org.springframework.util.PathMatcher;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.SaTokenManager;
|
||||||
|
import cn.dev33.satoken.autowired.SaTokenSpringAutowired;
|
||||||
|
import cn.dev33.satoken.fun.IsRunFunction;
|
||||||
|
import cn.dev33.satoken.fun.SaFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对路由匹配符相关操作的封装工具类
|
||||||
|
* @author kong
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SaRouterUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在进行路由匹配时所使用的的 PathMatcher 对象
|
||||||
|
*/
|
||||||
|
private static PathMatcher pathMatcher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 在进行路由匹配时所使用的的 PathMatcher 对象
|
||||||
|
*/
|
||||||
|
public static PathMatcher getPathMatcher() {
|
||||||
|
if(pathMatcher == null) {
|
||||||
|
pathMatcher = SaTokenSpringAutowired.pathMatcher;
|
||||||
|
if(pathMatcher == null) {
|
||||||
|
pathMatcher = new AntPathMatcher();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pathMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pathMatcher 写入: 在进行路由匹配时所使用的的 PathMatcher 对象
|
||||||
|
*/
|
||||||
|
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||||
|
SaRouterUtil.pathMatcher = pathMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- 路由匹配相关 --------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验指定路由匹配符是否可以匹配成功指定路径
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @param path 需要匹配的路径
|
||||||
|
* @return 是否匹配成功
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(String pattern, String path) {
|
||||||
|
if(getPathMatcher().match(pattern, path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验指定路由匹配符是否可以匹配成功指定路径
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @param path 需要匹配的路径集合
|
||||||
|
* @return 是否匹配成功
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(List<String> patterns, String path) {
|
||||||
|
for (String pattern : patterns) {
|
||||||
|
if(isMatch(pattern, path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验指定路由匹配符是否可以匹配成功当前URI
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @return 是否匹配成功
|
||||||
|
*/
|
||||||
|
public static boolean isMatchCurrURI(String pattern) {
|
||||||
|
return isMatch(pattern, SaTokenManager.getSaTokenServlet().getRequest().getRequestURI());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验指定路由匹配符是否可以匹配成功当前URI
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @return 是否匹配成功
|
||||||
|
*/
|
||||||
|
public static boolean isMatchCurrURI(List<String> patterns) {
|
||||||
|
return isMatch(patterns, SaTokenManager.getSaTokenServlet().getRequest().getRequestURI());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- 执行相关 --------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用路由匹配符与当前URI执行匹配,如果匹配成功则执行验证函数
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @param function 要执行的方法
|
||||||
|
*/
|
||||||
|
public static void match(String pattern, SaFunction function) {
|
||||||
|
if(isMatchCurrURI(pattern)) {
|
||||||
|
function.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用路由匹配符与当前URI执行匹配 (并指定排除匹配符),如果匹配成功则执行验证函数
|
||||||
|
* @param pattern 路由匹配符
|
||||||
|
* @param excludePattern 要排除的路由匹配符
|
||||||
|
* @param function 要执行的方法
|
||||||
|
*/
|
||||||
|
public static void match(String pattern, String excludePattern, SaFunction function) {
|
||||||
|
if(isMatchCurrURI(pattern)) {
|
||||||
|
if(isMatchCurrURI(excludePattern) == false) {
|
||||||
|
function.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用路由匹配符集合与当前URI执行匹配,如果匹配成功则执行验证函数
|
||||||
|
* @param patterns 路由匹配符集合
|
||||||
|
* @param function 要执行的方法
|
||||||
|
*/
|
||||||
|
public static void match(List<String> patterns, SaFunction function) {
|
||||||
|
if(isMatchCurrURI(patterns)) {
|
||||||
|
function.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用路由匹配符集合与当前URI执行匹配 (并指定排除匹配符),如果匹配成功则执行验证函数
|
||||||
|
* @param patterns 路由匹配符集合
|
||||||
|
* @param excludePatterns 要排除的路由匹配符集合
|
||||||
|
* @param function 要执行的方法
|
||||||
|
*/
|
||||||
|
public static void match(List<String> patterns, List<String> excludePatterns, SaFunction function) {
|
||||||
|
if(isMatchCurrURI(patterns)) {
|
||||||
|
if(isMatchCurrURI(excludePatterns) == false) {
|
||||||
|
function.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用路由匹配符集合与当前URI执行匹配,如果匹配成功则执行验证函数
|
||||||
|
* @param patterns 路由匹配符集合
|
||||||
|
* @return 匹配结果包装对象
|
||||||
|
*/
|
||||||
|
public static IsRunFunction match(String... patterns) {
|
||||||
|
boolean matchResult = isMatch(Arrays.asList(patterns), SaTokenManager.getSaTokenServlet().getRequest().getRequestURI());
|
||||||
|
return new IsRunFunction(matchResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user