From cfc7248466fe97dfd3bc29e29ac2e079761a0487 Mon Sep 17 00:00:00 2001 From: cat_shark <1716967236@qq.com> Date: Sat, 2 Aug 2025 12:08:08 +0800 Subject: [PATCH] =?UTF-8?q?fit=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=89=8D?= =?UTF-8?q?=E7=AB=AFcors=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/CorsProperties.java | 13 +++++++++++++ .../config/WebCorsConfig.java | 13 ++++++++++--- src/main/resources/application-local.yml | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/guo/learningprogresstracker/config/CorsProperties.java diff --git a/src/main/java/com/guo/learningprogresstracker/config/CorsProperties.java b/src/main/java/com/guo/learningprogresstracker/config/CorsProperties.java new file mode 100644 index 0000000..f2731f2 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/config/CorsProperties.java @@ -0,0 +1,13 @@ +package com.guo.learningprogresstracker.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +@ConfigurationProperties(prefix = "cors") +public class CorsProperties { + private String[] allowedOrigins; + +} diff --git a/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java b/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java index bbefb5b..5fbd691 100644 --- a/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java +++ b/src/main/java/com/guo/learningprogresstracker/config/WebCorsConfig.java @@ -1,20 +1,27 @@ 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.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.Arrays; + +@Slf4j @Configuration +@RequiredArgsConstructor public class WebCorsConfig implements WebMvcConfigurer { - @Value("${cors.allowed-origins}") - private String[] allowedOrigins; // 支持多个Origin配置 + private final CorsProperties corsProperties; @Override public void addCorsMappings(CorsRegistry registry) { + log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins())); registry.addMapping("/**") - .allowedOrigins(allowedOrigins) + .allowedOrigins(corsProperties.getAllowedOrigins()) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .allowCredentials(true); diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 71de647..7a99839 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -9,4 +9,6 @@ server: port: 5157 cors: - allowed-origins: http://localhost:5158 \ No newline at end of file + allowed-origins: + - http://localhost:5158 + - http://192.168.123.199:5158 \ No newline at end of file