diff --git a/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java b/src/main/java/com/guo/learningprogresstracker/config/DevWebCorsConfig.java similarity index 77% rename from src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java rename to src/main/java/com/guo/learningprogresstracker/config/DevWebCorsConfig.java index fbd16e3..51eead9 100644 --- a/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java +++ b/src/main/java/com/guo/learningprogresstracker/config/DevWebCorsConfig.java @@ -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()); diff --git a/src/main/java/com/guo/learningprogresstracker/config/ProdWebCorsConfig.java b/src/main/java/com/guo/learningprogresstracker/config/ProdWebCorsConfig.java new file mode 100644 index 0000000..e6b03df --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/config/ProdWebCorsConfig.java @@ -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); + } +}