diff --git a/sa-token-doc/use/route-check.md b/sa-token-doc/use/route-check.md index 59a0d62c..f036420e 100644 --- a/sa-token-doc/use/route-check.md +++ b/sa-token-doc/use/route-check.md @@ -225,7 +225,7 @@ public SaResult getList() { ### 7、关闭注解校验 -`SaInterceptor` 只要注册到项目中,默认就会打开注解校验,如果要关闭此能力,需要: +`SaInterceptor` 只要注册到项目中,默认就会打开注解校验,如果要关闭此能力,需要指定 `isAnnotation` 为 false: ``` java @Override @@ -238,6 +238,26 @@ public void addInterceptors(InterceptorRegistry registry) { } ``` + +你也可以使用 `setBeforeAuth` 注册认证前置函数: + +``` java +@Override +public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new SaInterceptor(handle -> { + System.out.println(1); + }) + .setBeforeAuth(handle -> { + System.out.println(2); + }) + ).addPathPatterns("/**"); +} +``` + +如上代码,先执行 2,再执行注解鉴权,再执行 1,如果 beforeAuth 里包含 `SaRouter.stop()` 将跳过后续的注解鉴权和 auth 认证环节。 + + + --- 参数:路由处理函数指针 */ + public SaParamFunction beforeAuth = handler -> {}; + + /** + * 认证函数:每次请求执行 + *

参数:路由处理函数指针 + */ public SaParamFunction auth = handler -> {}; /** @@ -68,18 +74,28 @@ public class SaInterceptor implements HandlerInterceptor { this.isAnnotation = isAnnotation; return this; } - + + /** + * 写入 [ 认证前置函数 ]: 在注解鉴权之前执行 + * @param beforeAuth / + * @return 对象自身 + */ + public SaInterceptor setBeforeAuth(SaParamFunction beforeAuth) { + this.beforeAuth = beforeAuth; + return this; + } + /** * 写入 [ 认证函数 ]: 每次请求执行 - * @param auth / - * @return 对象自身 + * @param auth / + * @return 对象自身 */ public SaInterceptor setAuth(SaParamFunction auth) { this.auth = auth; return this; } - - + + // ----------------- 验证方法 ----------------- /** @@ -91,6 +107,8 @@ public class SaInterceptor implements HandlerInterceptor { throws Exception { try { + // 前置函数:在注解鉴权之前执行 + beforeAuth.run(handler); // 这里必须确保 handler 是 HandlerMethod 类型时,才能进行注解鉴权 if(isAnnotation && handler instanceof HandlerMethod) {