package com.hxzk.gps.util.SaTokenConfigure;
|
|
import cn.dev33.satoken.interceptor.SaInterceptor;
|
import cn.dev33.satoken.stp.StpUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.web.servlet.LocaleResolver;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.Locale;
|
|
@Configuration
|
public class SaTokenConfigure implements WebMvcConfigurer {
|
// 注册拦截器
|
@Value("${upload.dir}")
|
private String uploadDir;
|
|
@Autowired
|
private LocaleChangeInterceptor localeChangeInterceptor;
|
|
@Override
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
// 获取上传文件目录的绝对路径
|
// 将外部目录映射到一个虚拟路径,如/images/**
|
registry.addResourceHandler("/uploads/**")
|
.addResourceLocations("file:" + uploadDir + "/");
|
}
|
|
@Override
|
public void addInterceptors(InterceptorRegistry registry) {
|
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
|
registry.addInterceptor(new SaInterceptor())
|
.addPathPatterns("/**")
|
.excludePathPatterns("/User/Login","/uploads/**","/uploads/**");
|
|
// 注册 LocaleChangeInterceptor,确保在SaInterceptor之后执行
|
registry.addInterceptor(localeChangeInterceptor)
|
.addPathPatterns("/**");
|
}
|
// 添加跨域配置
|
@Override
|
public void addCorsMappings(CorsRegistry registry) {
|
registry.addMapping("/**")
|
.allowedOriginPatterns("*") // 使用 allowedOriginPatterns 代替 allowedOrigins
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
.allowedHeaders("*")
|
.allowCredentials(true)
|
.maxAge(3600);
|
}
|
}
|