|
|
@@ -4,9 +4,13 @@ import cn.dev33.satoken.interceptor.SaInterceptor;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
@Slf4j
|
|
|
@Configuration
|
|
|
public class SaTokenConfig implements WebMvcConfigurer {
|
|
|
@@ -15,10 +19,16 @@ public class SaTokenConfig implements WebMvcConfigurer {
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
// 注册Sa-Token拦截器
|
|
|
registry.addInterceptor(new SaInterceptor(handle -> {
|
|
|
+ // 先尝试从URL参数获取token
|
|
|
+ String tokenFromParam = getTokenFromRequestParam();
|
|
|
+ if (tokenFromParam != null && !tokenFromParam.isEmpty()) {
|
|
|
+ log.info("[Sa-Token] 从URL参数读取到token: {}", maskToken(tokenFromParam));
|
|
|
+ }
|
|
|
+
|
|
|
String tokenValue = StpUtil.getTokenValue();
|
|
|
|
|
|
if (tokenValue != null && !tokenValue.isEmpty()) {
|
|
|
- log.info("[Sa-Token] 读取到token: {}", maskToken(tokenValue));
|
|
|
+ log.info("[Sa-Token] 当前token: {}", maskToken(tokenValue));
|
|
|
} else {
|
|
|
log.warn("[Sa-Token] 未找到token");
|
|
|
}
|
|
|
@@ -34,6 +44,22 @@ public class SaTokenConfig implements WebMvcConfigurer {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 从当前请求的URL参数中获取token
|
|
|
+ */
|
|
|
+ private String getTokenFromRequestParam() {
|
|
|
+ try {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (attributes != null) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ return request.getParameter("access_token");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.debug("从URL参数获取token失败: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 遮蔽token敏感信息,只显示前后几位
|
|
|
*/
|