69 lines
2.3 KiB
Java
69 lines
2.3 KiB
Java
|
|
package vvpkassistant.config;
|
||
|
|
|
||
|
|
import cn.dev33.satoken.fun.strategy.SaCorsHandleFunction;
|
||
|
|
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||
|
|
import cn.dev33.satoken.router.SaHttpMethod;
|
||
|
|
import cn.dev33.satoken.router.SaRouter;
|
||
|
|
import cn.dev33.satoken.stp.StpUtil;
|
||
|
|
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;
|
||
|
|
|
||
|
|
@Configuration
|
||
|
|
@Slf4j
|
||
|
|
public class SaTokenConfigure implements WebMvcConfigurer {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
||
|
|
// 注册Sa-Token的拦截器
|
||
|
|
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
|
||
|
|
.addPathPatterns("/**")
|
||
|
|
.excludePathPatterns(getExcludePaths());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取需要放行的路径
|
||
|
|
*/
|
||
|
|
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",
|
||
|
|
// 你的其他放行路径,例如登录接口
|
||
|
|
"/user/loginWithPhoneNumber",
|
||
|
|
"/user/registerWithMail",
|
||
|
|
"/user/loginWithMail"
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
@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();
|
||
|
|
};
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|