diff --git a/src/main/java/com/guo/learningprogresstracker/config/SaTokenConfigure.java b/src/main/java/com/guo/learningprogresstracker/config/SaTokenConfigure.java index fdd4c1b..f6fe506 100644 --- a/src/main/java/com/guo/learningprogresstracker/config/SaTokenConfigure.java +++ b/src/main/java/com/guo/learningprogresstracker/config/SaTokenConfigure.java @@ -4,9 +4,12 @@ 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.context.annotation.Profile; +import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +@Profile("prod") @Configuration @Slf4j public class SaTokenConfigure implements WebMvcConfigurer { @@ -19,4 +22,14 @@ public class SaTokenConfigure implements WebMvcConfigurer { .addPathPatterns("/**") .excludePathPatterns("/login"); } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } } diff --git a/src/main/java/com/guo/learningprogresstracker/config/SaTokenDevConfigure.java b/src/main/java/com/guo/learningprogresstracker/config/SaTokenDevConfigure.java new file mode 100644 index 0000000..049c648 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/config/SaTokenDevConfigure.java @@ -0,0 +1,35 @@ +package com.guo.learningprogresstracker.config; + +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.context.annotation.Profile; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Profile("dev") +@Configuration +@Slf4j +public class SaTokenDevConfigure implements WebMvcConfigurer { + + // 注册拦截器 + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。 + registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())) + .addPathPatterns("/**") + .excludePathPatterns("/login"); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } +}