Kaynağa Gözat

小程序调试

skyline 2 ay önce
ebeveyn
işleme
f8c7b9176e

+ 14 - 61
haha-miniapp/src/main/java/com/haha/miniapp/config/SaTokenConfig.java

@@ -2,10 +2,7 @@ package com.haha.miniapp.config;
 
 import cn.dev33.satoken.interceptor.SaInterceptor;
 import cn.dev33.satoken.stp.StpUtil;
-import cn.dev33.satoken.context.SaHolder;
-import cn.dev33.satoken.filter.SaServletFilter;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -14,73 +11,19 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Configuration
 public class SaTokenConfig implements WebMvcConfigurer {
 
-    /**
-     * 注册 Sa-Token 全局过滤器
-     * Sa-Token 会根据配置自动从请求头读取 token
-     */
-    @Bean
-    public SaServletFilter getSaServletFilter() {
-        return new SaServletFilter()
-            .addInclude("/**")
-            .addExclude("/login/**", "/health/**", "/callback/**")
-            .setAuth(obj -> {
-                // Sa-Token 会根据配置自动从请求头读取 token
-                // 这里只需要执行登录校验即可
-                String tokenValue = StpUtil.getTokenValue();
-                if (tokenValue != null && !tokenValue.isEmpty()) {
-                    log.info("[Sa-Token Filter] 读取到token: {}", maskToken(tokenValue));
-                } else {
-                    log.warn("[Sa-Token Filter] 未找到token");
-                }
-                
-                // 执行登录校验
-                StpUtil.checkLogin();
-            })
-            .setError(e -> {
-                log.warn("[Sa-Token Filter] 认证失败: {}", e.getMessage());
-                return e;
-            });
-    }
-    
-    /**
-     * 验证token是否有效
-     */
-    private boolean isValidToken(String token) {
-        return token != null && !token.trim().isEmpty() && !"null".equals(token);
-    }
-    
-    /**
-     * 遮蔽token敏感信息,只显示前后几位
-     */
-    private String maskToken(String token) {
-        if (token == null || token.length() <= 8) {
-            return "***";
-        }
-        return token.substring(0, 4) + "****" + token.substring(token.length() - 4);
-    }
-
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
-        // 注册Sa-Token拦截器,拦截所有路径
+        // 注册Sa-Token拦截器
         registry.addInterceptor(new SaInterceptor(handle -> {
-            // 记录详细的token信息(生产环境也启用INFO级别日志)
             String tokenValue = StpUtil.getTokenValue();
-            boolean isLogin = StpUtil.isLogin();
             
             if (tokenValue != null && !tokenValue.isEmpty()) {
-                String loginId = "unknown";
-                try {
-                    loginId = StpUtil.getLoginIdAsString();
-                } catch (Exception e) {
-                    // 忽略异常,使用默认值
-                }
-                log.info("[Sa-Token Interceptor] 当前请求token: {}, 是否登录: {}, 登录账号: {}", 
-                    maskToken(tokenValue), isLogin, isLogin ? loginId : "未登录");
+                log.info("[Sa-Token] 读取到token: {}", maskToken(tokenValue));
             } else {
-                log.info("[Sa-Token Interceptor] 当前请求未携带有效token");
+                log.warn("[Sa-Token] 未找到token");
             }
 
-            // 登录认证:除了指定的接口,其他都需要登录
+            // 执行登录校验
             StpUtil.checkLogin();
         }))
         .addPathPatterns("/**")
@@ -90,4 +33,14 @@ public class SaTokenConfig implements WebMvcConfigurer {
             "/callback/**"         // 第三方回调接口
         );
     }
+    
+    /**
+     * 遮蔽token敏感信息,只显示前后几位
+     */
+    private String maskToken(String token) {
+        if (token == null || token.length() <= 8) {
+            return "***";
+        }
+        return token.substring(0, 4) + "****" + token.substring(token.length() - 4);
+    }
 }

+ 9 - 2
haha-mp/src/pages/index/index.vue

@@ -37,9 +37,16 @@
 </template>
 
 <script setup lang="ts">
-import { ref } from 'vue'
+import { ref, onMounted } from 'vue'
+import { onShow } from '@dcloudio/uni-app'
 import { scanDoor } from '../../api/device'
-import { isLoggedIn } from '../../utils/auth'
+import { isLoggedIn, getToken } from '../../utils/auth'
+
+// 页面显示时检查token
+onShow(() => {
+  const token = getToken()
+  console.log('[首页 onShow] token状态:', token ? '存在' : '不存在')
+})
 
 const scanCode = () => {
   // 检查登录状态