From 34567f7edfc024ca851928fd8032a5993be00ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Wed, 10 Apr 2024 22:19:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CORS=20=EC=84=A4=EC=A0=95=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/in/koreatech/koin/KoinApplication.java | 2 ++ .../koreatech/koin/global/config/CorsProperties.java | 12 ++++++++++++ .../in/koreatech/koin/global/config/WebConfig.java | 12 ++++++++++++ src/main/resources/application-test.yml | 4 ++++ 4 files changed, 30 insertions(+) create mode 100644 src/main/java/in/koreatech/koin/global/config/CorsProperties.java diff --git a/src/main/java/in/koreatech/koin/KoinApplication.java b/src/main/java/in/koreatech/koin/KoinApplication.java index fa79177c5..6908a96b4 100644 --- a/src/main/java/in/koreatech/koin/KoinApplication.java +++ b/src/main/java/in/koreatech/koin/KoinApplication.java @@ -2,10 +2,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication +@ConfigurationPropertiesScan public class KoinApplication { public static void main(String[] args) { diff --git a/src/main/java/in/koreatech/koin/global/config/CorsProperties.java b/src/main/java/in/koreatech/koin/global/config/CorsProperties.java new file mode 100644 index 000000000..903205a08 --- /dev/null +++ b/src/main/java/in/koreatech/koin/global/config/CorsProperties.java @@ -0,0 +1,12 @@ +package in.koreatech.koin.global.config; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "cors") +public record CorsProperties( + List allowedOrigins +) { + +} diff --git a/src/main/java/in/koreatech/koin/global/config/WebConfig.java b/src/main/java/in/koreatech/koin/global/config/WebConfig.java index 951f30ea8..ec7e2afda 100644 --- a/src/main/java/in/koreatech/koin/global/config/WebConfig.java +++ b/src/main/java/in/koreatech/koin/global/config/WebConfig.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -31,6 +32,7 @@ public class WebConfig implements WebMvcConfigurer { private final IpAddressInterceptor ipAddressInterceptor; private final ServerURLArgumentResolver serverURLArgumentResolver; private final ServerURLInterceptor serverURLInterceptor; + private final CorsProperties corsProperties; @Override public void addInterceptors(InterceptorRegistry registry) { @@ -59,4 +61,14 @@ public void addFormatters(FormatterRegistry registry) { registry.addConverter(new BusStationEnumConverter()); registry.addConverter(new ImageUploadDomainEnumConverter()); } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins(corsProperties.allowedOrigins().toArray(new String[0])) + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } } diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index 6c397be09..42d4281f5 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -62,3 +62,7 @@ fcm: url: koinAppUrl:// OPEN_API_KEY: + +cors: + allowedOrigins: + - http://localhost:3000