feat(N/A): 修改cors配置

This commit is contained in:
guo
2025-10-26 14:45:23 +08:00
parent 56a0820fc2
commit a18156af46
2 changed files with 33 additions and 4 deletions
@@ -2,18 +2,18 @@ package com.guo.learningprogresstracker.config;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 WebCorsConfig implements WebMvcConfigurer {
public class DevWebCorsConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
@@ -21,7 +21,7 @@ public class WebCorsConfig implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins()));
registry.addMapping("/**")
.allowedOrigins(corsProperties.getAllowedOrigins())
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(null == corsProperties.getAllowCredentials() || corsProperties.getAllowCredentials());
@@ -0,0 +1,29 @@
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("prod")
@Slf4j
@Configuration
@RequiredArgsConstructor
public class ProdWebCorsConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins()));
registry.addMapping("/**")
.allowedOrigins(corsProperties.getAllowedOrigins())
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
}