2025-06-12 13:31:52 +08:00
|
|
|
package com.yupi.springbootinit.config;
|
|
|
|
|
|
2025-06-23 13:03:55 +08:00
|
|
|
import cn.dev33.satoken.fun.strategy.SaCorsHandleFunction;
|
2025-06-12 13:31:52 +08:00
|
|
|
import cn.dev33.satoken.interceptor.SaInterceptor;
|
2025-06-23 13:03:55 +08:00
|
|
|
import cn.dev33.satoken.router.SaHttpMethod;
|
|
|
|
|
import cn.dev33.satoken.router.SaRouter;
|
2025-06-12 13:31:52 +08:00
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
2025-09-09 15:36:47 +08:00
|
|
|
import com.yupi.springbootinit.Interceptor.TokenInterceptor;
|
2025-06-23 13:03:55 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
2025-06-12 13:31:52 +08:00
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
2025-06-23 13:03:55 +08:00
|
|
|
@Slf4j
|
2025-06-12 13:31:52 +08:00
|
|
|
public class SaTokenConfigure implements WebMvcConfigurer {
|
2025-06-20 20:15:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-12 13:31:52 +08:00
|
|
|
@Override
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
// 注册Sa-Token的拦截器
|
2025-06-12 20:25:15 +08:00
|
|
|
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
|
2025-06-23 13:03:55 +08:00
|
|
|
.addPathPatterns("/**")
|
2025-06-12 20:25:15 +08:00
|
|
|
.excludePathPatterns(getExcludePaths());
|
2025-09-09 15:36:47 +08:00
|
|
|
registry.addInterceptor(new TokenInterceptor())
|
|
|
|
|
.addPathPatterns("/**")
|
|
|
|
|
.excludePathPatterns(getExcludePaths());
|
2025-06-12 13:31:52 +08:00
|
|
|
}
|
2025-09-09 15:36:47 +08:00
|
|
|
|
2025-06-12 13:31:52 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取需要放行的路径
|
|
|
|
|
*/
|
|
|
|
|
private String[] getExcludePaths() {
|
|
|
|
|
return new String[]{
|
|
|
|
|
// Swagger & Knife4j 相关
|
|
|
|
|
"/doc.html",
|
|
|
|
|
"/webjars/**",
|
|
|
|
|
"/swagger-resources/**",
|
|
|
|
|
"/v2/api-docs",
|
|
|
|
|
"/v3/api-docs",
|
|
|
|
|
"/v3/api-docs/**",
|
|
|
|
|
"/swagger-ui.html",
|
|
|
|
|
"/swagger-ui/**",
|
|
|
|
|
"/favicon.ico",
|
|
|
|
|
// 你的其他放行路径,例如登录接口
|
2025-06-20 19:22:25 +08:00
|
|
|
"/user/doLogin",
|
2025-06-25 21:55:49 +08:00
|
|
|
"/tenant/get-id-by-name",
|
2025-07-03 15:25:33 +08:00
|
|
|
"/user/bigbrother-doLogin",
|
2025-08-25 14:28:17 +08:00
|
|
|
"/user/aiChat-doLogin",
|
2025-08-27 16:42:53 +08:00
|
|
|
"/user/aiChat-logout",
|
|
|
|
|
"/error",
|
2025-06-12 13:31:52 +08:00
|
|
|
};
|
|
|
|
|
}
|
2025-06-23 13:03:55 +08:00
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public SaCorsHandleFunction corsHandle() {
|
|
|
|
|
return (req, res, sto) -> {
|
|
|
|
|
res.
|
|
|
|
|
// 允许指定域访问跨域资源
|
|
|
|
|
setHeader("Access-Control-Allow-Origin", "*")
|
|
|
|
|
// 允许所有请求方式
|
|
|
|
|
.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
|
|
|
|
|
// 有效时间
|
|
|
|
|
.setHeader("Access-Control-Max-Age", "3600")
|
|
|
|
|
// 允许的header参数
|
|
|
|
|
.setHeader("Access-Control-Allow-Headers", "*");
|
|
|
|
|
|
|
|
|
|
// 如果是预检请求,则立即返回到前端
|
|
|
|
|
SaRouter.match(SaHttpMethod.OPTIONS)
|
|
|
|
|
.back();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2025-06-12 13:31:52 +08:00
|
|
|
}
|