From c265df1bfccb5a6415b6b87aaa114fced93369ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9D=80=EC=B1=84?= Date: Mon, 29 Apr 2024 11:05:10 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20cors=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/config/WebSecurityConfig.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/dnaaaaahtac/wooriforei/global/config/WebSecurityConfig.java b/src/main/java/dnaaaaahtac/wooriforei/global/config/WebSecurityConfig.java index f63bb8c..e9d8a8d 100644 --- a/src/main/java/dnaaaaahtac/wooriforei/global/config/WebSecurityConfig.java +++ b/src/main/java/dnaaaaahtac/wooriforei/global/config/WebSecurityConfig.java @@ -15,6 +15,8 @@ import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.CorsConfigurationSource; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import java.util.List; @@ -36,6 +38,23 @@ public JwtAuthorizationFilter jwtAuthorizationFilter() { return new JwtAuthorizationFilter(jwtUtil, userDetailsService, objectMapper); } + @Bean + public CorsConfigurationSource corsConfigurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + configuration.setAllowCredentials(true); + + // TODO: 실제 배포 환경에서는 구체적인 도메인으로 제한해야 함 + configuration.addAllowedOrigin("http://localhost:3000"); + + configuration.addAllowedHeader("*"); + configuration.addAllowedMethod("*"); + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + + return source; + } + @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http @@ -54,7 +73,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .addFilterBefore(jwtAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class) // JWT 인증 필터 추가 .cors(cors -> cors.configurationSource(request -> { CorsConfiguration corsConfiguration = new CorsConfiguration(); - corsConfiguration.setAllowedOrigins(List.of("http://localhost:8080", "https://www.wooriforei.info", "localhost:3000")); + corsConfiguration.setAllowedOrigins(List.of("http://localhost:8080", "https://www.wooriforei.info", "http://localhost:3000")); corsConfiguration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); corsConfiguration.setAllowedHeaders(List.of("*")); return corsConfiguration;