feat(N/A): 配置WebMvcConfig,解决跨域问题

This commit is contained in:
guo
2025-10-26 18:34:23 +08:00
parent 6fc6815563
commit 4e01a28435
4 changed files with 35 additions and 56 deletions
@@ -1,29 +0,0 @@
package com.guo.learningprogresstracker.config;
import lombok.RequiredArgsConstructor;
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.WebMvcConfigurer;
import java.util.Arrays;
@Profile("dev")
@Slf4j
@Configuration
@RequiredArgsConstructor
public class DevWebCorsConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins()));
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(null == corsProperties.getAllowCredentials() || corsProperties.getAllowCredentials());
}
}
@@ -1,24 +0,0 @@
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Profile("prod")
@Configuration
@Slf4j
public class SaTokenConfigure implements WebMvcConfigurer {
// 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.config;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@@ -10,10 +11,18 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays;
/**
* @author Administrator
*/
@Profile("dev")
@Configuration
@Slf4j
public class SaTokenDevConfigure implements WebMvcConfigurer {
@RequiredArgsConstructor
public class WebMvcDevConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
// 注册拦截器
@Override
@@ -28,4 +37,14 @@ public class SaTokenDevConfigure implements WebMvcConfigurer {
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins()));
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(null == corsProperties.getAllowCredentials() || corsProperties.getAllowCredentials());
}
}
@@ -1,19 +1,22 @@
package com.guo.learningprogresstracker.config;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
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;
import java.util.Arrays;
@Profile("prod")
@Slf4j
@Configuration
@RequiredArgsConstructor
public class ProdWebCorsConfig implements WebMvcConfigurer {
@Slf4j
public class WebMvcProdConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
@@ -26,4 +29,14 @@ public class ProdWebCorsConfig implements WebMvcConfigurer {
.allowedHeaders("*")
.allowCredentials(true);
}
// 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册 Sa-Token 拦截器校验规则为 StpUtil.checkLogin() 登录校验
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}