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