From 76bafa5770c56fd4eeb42075dc707d5ab1b8540c Mon Sep 17 00:00:00 2001 From: JIHO LEE <161289673+GitJIHO@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:02:28 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20CORS=20=ED=94=84=EB=A1=A0=ED=8A=B8?= =?UTF-8?q?=20=EA=B0=9C=EB=B0=9C=ED=99=98=EA=B2=BD=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?(#241)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor: CORS 프론트 개발주소 추가 --- .../sinitto/common/config/WebConfig.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/sinitto/common/config/WebConfig.java b/src/main/java/com/example/sinitto/common/config/WebConfig.java index 29547a4..5a881fb 100644 --- a/src/main/java/com/example/sinitto/common/config/WebConfig.java +++ b/src/main/java/com/example/sinitto/common/config/WebConfig.java @@ -1,6 +1,7 @@ package com.example.sinitto.common.config; import com.example.sinitto.common.interceptor.JwtInterceptor; +import com.example.sinitto.common.properties.KakaoProperties; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; @@ -9,6 +10,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import org.springframework.web.cors.CorsConfiguration; @@ -25,10 +27,14 @@ public class WebConfig implements WebMvcConfigurer { private static final int TIME_OUT_DURATION = 5; private static final int MAX_OPEN_CONNECTIONS = 100; private static final int CONNECTIONS_PER_IP_PORT_PAIR = 5; + private static final String DEV_SERVER_URL = "https://localhost:5173"; + private final JwtInterceptor jwtInterceptor; + private final KakaoProperties kakaoProperties; - public WebConfig(JwtInterceptor jwtInterceptor) { + public WebConfig(JwtInterceptor jwtInterceptor, KakaoProperties kakaoProperties) { this.jwtInterceptor = jwtInterceptor; + this.kakaoProperties = kakaoProperties; } @Bean @@ -61,10 +67,21 @@ public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); - config.addAllowedOriginPattern("https://www.sinitto.life"); - config.addAllowedHeader("*"); - config.addAllowedMethod("*"); + + config.addAllowedOriginPattern(kakaoProperties.frontUri()); + config.addAllowedOriginPattern(DEV_SERVER_URL); + + config.addAllowedMethod(HttpMethod.GET); + config.addAllowedMethod(HttpMethod.POST); + config.addAllowedMethod(HttpMethod.PUT); + config.addAllowedMethod(HttpMethod.DELETE); + config.addAllowedMethod(HttpMethod.OPTIONS); + + config.addAllowedHeader("Authorization"); + config.addAllowedHeader("Content-Type"); + config.addExposedHeader("Authorization"); + config.setMaxAge(3600L); source.registerCorsConfiguration("/**", config); return new CorsFilter(source);