mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-21 02:57:23 +08:00
SaRouterUtil
迁移到core核心包,优化依赖架构
This commit is contained in:
@@ -105,19 +105,15 @@ public class SaTokenSpringAutowired {
|
||||
SaTokenManager.setSaTokenServlet(saTokenServlet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由匹配器
|
||||
*/
|
||||
public static PathMatcher pathMatcher;
|
||||
|
||||
/**
|
||||
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||
*
|
||||
* @param pathMatcher 要设置的 pathMatcher
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaTokenSpringAutowired.pathMatcher = pathMatcher;
|
||||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaTokenServletSpringImpl.setPathMatcher(pathMatcher);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,23 +0,0 @@
|
||||
package cn.dev33.satoken.interceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 执行验证方法的辅助类
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public interface SaRouteFunction {
|
||||
|
||||
/**
|
||||
* 执行验证的方法
|
||||
*
|
||||
* @param request request对象
|
||||
* @param response response对象
|
||||
* @param handler 处理对象
|
||||
*/
|
||||
public void run(HttpServletRequest request, HttpServletResponse response, Object handler);
|
||||
|
||||
}
|
@@ -5,8 +5,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.router.SaRouteFunction;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
@@ -15,204 +14,25 @@ import cn.dev33.satoken.stp.StpUtil;
|
||||
*/
|
||||
public class SaRouteInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
// ----------------- 属性 -----------------
|
||||
|
||||
/**
|
||||
* 底层的 StpLogic 对象
|
||||
*/
|
||||
private StpLogic stpLogic;
|
||||
|
||||
/**
|
||||
* 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
|
||||
*/
|
||||
private int type;
|
||||
|
||||
/**
|
||||
* 验证模式 AND | OR
|
||||
*/
|
||||
private SaMode mode;
|
||||
|
||||
/**
|
||||
* 标识码数组
|
||||
*/
|
||||
private String[] codes;
|
||||
|
||||
/**
|
||||
* 自定义模式下的执行函数
|
||||
*/
|
||||
private SaRouteFunction function;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 表示登录验证
|
||||
*/
|
||||
public static final int LOGIN = 1;
|
||||
|
||||
/**
|
||||
* 表示角色验证
|
||||
*/
|
||||
public static final int ROLE = 2;
|
||||
|
||||
/**
|
||||
* 表示权限验证
|
||||
*/
|
||||
public static final int PERMISSION = 3;
|
||||
|
||||
/**
|
||||
* 表示自定义验证
|
||||
*/
|
||||
public static final int CUSTOM = 4;
|
||||
|
||||
|
||||
/**
|
||||
* @return 底层的 StpLogic 对象
|
||||
*/
|
||||
public StpLogic getStpLogic() {
|
||||
if(stpLogic == null) {
|
||||
stpLogic = StpUtil.stpLogic;
|
||||
}
|
||||
return stpLogic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stpLogic 底层的 StpLogic 对象
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaRouteInterceptor setStpLogic(StpLogic stpLogic) {
|
||||
this.stpLogic = stpLogic;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaRouteInterceptor setType(int type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 验证模式 AND | OR
|
||||
*/
|
||||
public SaMode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mode 验证模式 AND | OR
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaRouteInterceptor setMode(SaMode mode) {
|
||||
this.mode = mode;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 标识码数组
|
||||
*/
|
||||
public String[] getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param codes 标识码数组
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaRouteInterceptor setCodes(String... codes) {
|
||||
this.codes = codes;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 自定义模式下的执行函数
|
||||
*/
|
||||
public SaRouteFunction getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param function 设置自定义模式下的执行函数
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaRouteInterceptor setFunction(SaRouteFunction function) {
|
||||
this.type = SaRouteInterceptor.CUSTOM;
|
||||
this.function = function;
|
||||
return this;
|
||||
}
|
||||
|
||||
// ----------------- 构建相关 -----------------
|
||||
|
||||
/**
|
||||
* 创建 (全参数)
|
||||
* @param type 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
|
||||
* @param mode 验证模式 AND | OR
|
||||
* @param codes 标识码数组
|
||||
*/
|
||||
public SaRouteInterceptor(int type, SaMode mode, String... codes) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.mode = mode;
|
||||
this.codes = codes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 (默认为登录验证)
|
||||
*/
|
||||
public SaRouteInterceptor() {
|
||||
this(SaRouteInterceptor.LOGIN, null, new String[0]);
|
||||
}
|
||||
public SaRouteFunction function;
|
||||
|
||||
/**
|
||||
* 创建 (默认为自定义认证)
|
||||
* @param function 自定义模式下的执行函数
|
||||
*/
|
||||
public SaRouteInterceptor(SaRouteFunction function) {
|
||||
this(SaRouteInterceptor.CUSTOM, null, new String[0]);
|
||||
setFunction(function);
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个模式为登录认证的sa路由拦截器
|
||||
* @return sa拦截器
|
||||
*/
|
||||
public static SaRouteInterceptor createLoginVal() {
|
||||
return new SaRouteInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个模式为角色认证的sa路由拦截器
|
||||
* @param roles 需要验证的角色标识列表
|
||||
* @return sa拦截器
|
||||
*/
|
||||
public static SaRouteInterceptor createRoleVal(String... roles) {
|
||||
return new SaRouteInterceptor(SaRouteInterceptor.ROLE, SaMode.AND, roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个模式为权限认证的sa路由拦截器
|
||||
* @param permissions 需要验证的权限列表
|
||||
* @return sa拦截器
|
||||
*/
|
||||
public static SaRouteInterceptor createPermissionVal(String... permissions) {
|
||||
return new SaRouteInterceptor(SaRouteInterceptor.PERMISSION, SaMode.AND, permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个模式为自定义认证的sa路由拦截器
|
||||
* 静态方法快速构建一个
|
||||
* @param function 自定义模式下的执行函数
|
||||
* @return sa拦截器
|
||||
* @return sa路由拦截器
|
||||
*/
|
||||
public static SaRouteInterceptor createCustomVal(SaRouteFunction function) {
|
||||
public static SaRouteInterceptor newInstance(SaRouteFunction function) {
|
||||
return new SaRouteInterceptor(function);
|
||||
}
|
||||
|
||||
@@ -226,22 +46,10 @@ public class SaRouteInterceptor implements HandlerInterceptor {
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
|
||||
// 根据模式进行验证
|
||||
if(this.type == SaRouteInterceptor.LOGIN) {
|
||||
getStpLogic().checkLogin();
|
||||
} else if(this.type == SaRouteInterceptor.ROLE) {
|
||||
if(mode == SaMode.AND) {
|
||||
getStpLogic().checkRoleAnd(codes);
|
||||
} else {
|
||||
getStpLogic().checkRoleOr(codes);
|
||||
}
|
||||
} else if(this.type == SaRouteInterceptor.PERMISSION) {
|
||||
if(mode == SaMode.AND) {
|
||||
getStpLogic().checkPermissionAnd(codes);
|
||||
} else {
|
||||
getStpLogic().checkPermissionOr(codes);
|
||||
}
|
||||
} else if(this.type == SaRouteInterceptor.CUSTOM) {
|
||||
// 如果未提供function,默认进行登录验证
|
||||
if(function == null) {
|
||||
StpUtil.checkLogin();
|
||||
} else {
|
||||
function.run(request, response, handler);
|
||||
}
|
||||
|
||||
@@ -250,6 +58,4 @@ public class SaRouteInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,163 +0,0 @@
|
||||
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 patterns 路由匹配符
|
||||
* @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 patterns 路由匹配符
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -3,10 +3,13 @@ package cn.dev33.satoken.spring;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
import cn.dev33.satoken.servlet.SaTokenServlet;
|
||||
|
||||
/**
|
||||
* sa-token 对cookie的相关操作 接口实现类
|
||||
* sa-token 对Cookie的相关操作 接口实现类
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
@@ -29,4 +32,37 @@ public class SaTokenServletSpringImpl implements SaTokenServlet {
|
||||
return SpringMVCUtil.getResponse();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 路由匹配器
|
||||
*/
|
||||
private static PathMatcher pathMatcher;
|
||||
|
||||
/**
|
||||
* 获取路由匹配器
|
||||
* @param pathMatcher 路由匹配器
|
||||
*/
|
||||
public static PathMatcher getPathMatcher() {
|
||||
if(pathMatcher == null) {
|
||||
pathMatcher = new AntPathMatcher();
|
||||
}
|
||||
return pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入路由匹配器
|
||||
* @param pathMatcher 路由匹配器
|
||||
*/
|
||||
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaTokenServletSpringImpl.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验指定路由匹配符是否可以匹配成功指定路径
|
||||
*/
|
||||
@Override
|
||||
public boolean matchPath(String pattern, String path) {
|
||||
return getPathMatcher().match(pattern, path);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user