Преглед изворни кода

fix: @SaIgnore 失效导致停车券接口返回无效 token,增强鉴权拦截器注解检查

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline пре 22 часа
родитељ
комит
ecfc25fcd7

+ 11 - 4
car-wash-miniapp/src/main/java/com/kym/miniapp/config/SaTokenConfigure.java

@@ -1,8 +1,10 @@
 package com.kym.miniapp.config;
 
+import cn.dev33.satoken.annotation.SaIgnore;
 import cn.dev33.satoken.interceptor.SaInterceptor;
 import cn.dev33.satoken.stp.StpUtil;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@@ -13,13 +15,18 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  */
 @Configuration
 public class SaTokenConfigure implements WebMvcConfigurer {
-    // 注册拦截器
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
-        // 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
-        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
+        registry.addInterceptor(new SaInterceptor(handler -> {
+                    if (handler instanceof HandlerMethod hm) {
+                        if (hm.hasMethodAnnotation(SaIgnore.class)
+                                || hm.getBeanType().isAnnotationPresent(SaIgnore.class)) {
+                            return;
+                        }
+                    }
+                    StpUtil.checkLogin();
+                }))
                 .addPathPatterns("/**")
-                // 以下接口不鉴权 更新:弃用,使用@SaIgnore注解替代
                 .excludePathPatterns("/parking-coupon/**");
     }