From 20519e755a4d63db2c00b2780a0ecea751196e4c Mon Sep 17 00:00:00 2001 From: Arachne <66822642+Arachneee@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:04:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CORS=20=EC=84=A4=EC=A0=95=20(#90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: CORS 설정 * refactor: CORS 설정 분리 * test: yml에 CORS 설정 추가 * feat: Origin 추가 * feat: Cors 설정에 HTTP OPTIONS 메서드 추가 --------- Co-authored-by: juha --- .../server/haengdong/config/WebConfig.java | 27 +++++++++++++++++++ server/src/main/resources/application.yml | 4 +++ server/src/test/resources/application.yml | 3 +++ 3 files changed, 34 insertions(+) create mode 100644 server/src/main/java/server/haengdong/config/WebConfig.java diff --git a/server/src/main/java/server/haengdong/config/WebConfig.java b/server/src/main/java/server/haengdong/config/WebConfig.java new file mode 100644 index 000000000..129d8b790 --- /dev/null +++ b/server/src/main/java/server/haengdong/config/WebConfig.java @@ -0,0 +1,27 @@ +package server.haengdong.config; + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@RequiredArgsConstructor +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Value("${cors.max-age}") + private Long maxAge; + + @Value("${cors.allowed-origins}") + private String[] allowedOrigins; + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins(allowedOrigins) + .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS") + .allowedHeaders("*") + .maxAge(maxAge); + } +} diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index aca841de8..e1bdd7f79 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -14,6 +14,10 @@ spring: format_sql: true show-sql: true +cors: + max-age: 3600 + allowed-origins: http://localhost:3000, https://haengdong.pro, https://dev.haengdong.pro, https://app.haengdong.pro + --- spring: diff --git a/server/src/test/resources/application.yml b/server/src/test/resources/application.yml index 49c481eaa..2d02b2712 100644 --- a/server/src/test/resources/application.yml +++ b/server/src/test/resources/application.yml @@ -13,3 +13,6 @@ spring: format_sql: true hibernate: ddl-auto: create-drop +cors: + max-age: 3600 + allowed-origins: http://localhost:8080