|
|
@@ -4,13 +4,9 @@ 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 {
|
|
|
@@ -19,20 +15,12 @@ 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()) {
|
|
|
- // 将token设置到当前上下文中
|
|
|
- StpUtil.setTokenValue(tokenFromParam);
|
|
|
- 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");
|
|
|
+ log.warn("[Sa-Token] 未找到token");
|
|
|
}
|
|
|
|
|
|
// 执行登录校验
|
|
|
@@ -46,22 +34,6 @@ 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敏感信息,只显示前后几位
|
|
|
*/
|