From eacd6d0b136643c45aa293991b878145b561a4ff Mon Sep 17 00:00:00 2001 From: sdw504512 Date: Sat, 16 Mar 2024 06:57:20 +0900 Subject: [PATCH] =?UTF-8?q?Bug:=20Cors=20=EC=98=A4=EB=A5=98=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/senity/waved/base/config/CorsConfig.java | 2 +- .../senity/waved/base/config/WebSecurityConfig.java | 13 ++++++++++++- src/main/resources/application.yml | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/senity/waved/base/config/CorsConfig.java b/src/main/java/com/senity/waved/base/config/CorsConfig.java index c9049fc..1a9031d 100644 --- a/src/main/java/com/senity/waved/base/config/CorsConfig.java +++ b/src/main/java/com/senity/waved/base/config/CorsConfig.java @@ -8,6 +8,7 @@ @Configuration public class CorsConfig { + @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); @@ -18,7 +19,6 @@ public CorsFilter corsFilter() { config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); - return new CorsFilter(source); } } \ No newline at end of file diff --git a/src/main/java/com/senity/waved/base/config/WebSecurityConfig.java b/src/main/java/com/senity/waved/base/config/WebSecurityConfig.java index 4543ae5..f1551d6 100644 --- a/src/main/java/com/senity/waved/base/config/WebSecurityConfig.java +++ b/src/main/java/com/senity/waved/base/config/WebSecurityConfig.java @@ -16,11 +16,13 @@ import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.filter.CorsFilter; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebSecurity @RequiredArgsConstructor -public class WebSecurityConfig { +public class WebSecurityConfig implements WebMvcConfigurer { private final TokenProvider tokenProvider; private final CorsFilter corsFilter; private final CustomOAuth2UserService customOAuth2UserService; @@ -57,4 +59,13 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti return http.build(); } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("https://localhost:3000") // 허용할 도메인을 지정합니다 + .allowedMethods("GET", "POST", "PUT", "DELETE") // 허용할 HTTP 메서드를 지정합니다 + .allowedHeaders("*") // 허용할 요청 헤더를 지정합니다 + .allowCredentials(true); // 인증 정보를 포함할지 여부를 지정합니다 + } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index dcf046c..475ea7f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -47,6 +47,7 @@ spring: registration: google: client-name: Google + redirect-uri: http://localhost:3000/oauth scope: - email - profile