调整 sa-token 拦截适配的处理顺序

This commit is contained in:
noear 2023-01-15 11:45:31 +08:00
parent 34f63449f7
commit 228c709d41
3 changed files with 72 additions and 46 deletions

View File

@ -173,25 +173,17 @@ public class SaTokenFilter implements Filter { //之所以改名,为了跟 SaT
Handler mainHandler = Solon.app().router().matchMain(ctx); Handler mainHandler = Solon.app().router().matchMain(ctx);
Action action = (mainHandler instanceof Action ? (Action) mainHandler : null); Action action = (mainHandler instanceof Action ? (Action) mainHandler : null);
//1.路径规则处理包括了静态文件 //先路径过滤下包括了静态文件
SaRouter.match(includeList).notMatch(excludeList).check(r -> { SaRouter.match(includeList).notMatch(excludeList).check(r -> {
beforeAuth.run(mainHandler); //1.执行前置处理主要是一些跨域之类的
if(beforeAuth != null) {
beforeAuth.run(mainHandler);
}
//2.执行注解处理
authAnno(action);
//3.执行规则处理
auth.run(mainHandler); auth.run(mainHandler);
}); });
//2.验证注解处理
if (isAnnotation && action != null) {
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
} catch (StopMatchException e) { } catch (StopMatchException e) {
} catch (SaTokenException e) { } catch (SaTokenException e) {
@ -213,4 +205,20 @@ public class SaTokenFilter implements Filter { //之所以改名,为了跟 SaT
chain.doFilter(ctx); chain.doFilter(ctx);
} }
private void authAnno(Action action) {
//2.验证注解处理
if (isAnnotation && action != null) {
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
}
} }

View File

@ -172,26 +172,18 @@ public class SaTokenInterceptor implements RouterInterceptor {
try { try {
Action action = (mainHandler instanceof Action ? (Action) mainHandler : null); Action action = (mainHandler instanceof Action ? (Action) mainHandler : null);
//1.路径规则处理 //先路径过滤下包括了静态文件
SaRouter.match(includeList).notMatch(excludeList).check(r -> { SaRouter.match(includeList).notMatch(excludeList).check(r -> {
beforeAuth.run(mainHandler); //1.执行前置处理主要是一些跨域之类的
if(beforeAuth != null) {
beforeAuth.run(mainHandler);
}
//2.执行注解处理
authAnno(action);
//3.执行规则处理
auth.run(mainHandler); auth.run(mainHandler);
}); });
//2.验证注解处理
if (isAnnotation && action != null) {
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
} catch (StopMatchException e) { } catch (StopMatchException e) {
} catch (SaTokenException e) { } catch (SaTokenException e) {
@ -213,4 +205,20 @@ public class SaTokenInterceptor implements RouterInterceptor {
chain.doIntercept(ctx, mainHandler); chain.doIntercept(ctx, mainHandler);
} }
private void authAnno(Action action) {
//2.验证注解处理
if (isAnnotation && action != null) {
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
}
} }

View File

@ -173,23 +173,17 @@ public class SaTokenPathInterceptor implements Handler {
//1.路径规则处理 //1.路径规则处理
SaRouter.match(includeList).notMatch(excludeList).check(r -> { SaRouter.match(includeList).notMatch(excludeList).check(r -> {
beforeAuth.run(action); //1.执行前置处理主要是一些跨域之类的
if(beforeAuth != null) {
beforeAuth.run(action);
}
//2.执行注解处理
authAnno(action);
//3.执行规则处理
auth.run(action); auth.run(action);
}); });
//2.验证注解处理
if(isAnnotation && action != null){
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if(SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
} catch (StopMatchException e) { } catch (StopMatchException e) {
} catch (SaTokenException e) { } catch (SaTokenException e) {
@ -202,10 +196,26 @@ public class SaTokenPathInterceptor implements Handler {
} }
// 2. 写入输出流 // 2. 写入输出流
if(result != null) { if (result != null) {
ctx.render(result); ctx.render(result);
} }
ctx.setHandled(true); ctx.setHandled(true);
} }
} }
private void authAnno(Action action) {
//2.验证注解处理
if (isAnnotation && action != null) {
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}
}
} }