From 448bd77eff19f0da23a34e4b0ba8feeea05bde40 Mon Sep 17 00:00:00 2001 From: jiyunio Date: Fri, 15 Nov 2024 02:59:49 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20Cors=20=EC=97=90=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/study/config/SecurityConfig.java | 27 +------------------ .../com/basic/study/config/WebConfig.java | 20 ++++++++++++++ 2 files changed, 21 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/basic/study/config/WebConfig.java diff --git a/src/main/java/com/basic/study/config/SecurityConfig.java b/src/main/java/com/basic/study/config/SecurityConfig.java index 44454fc..2de06c8 100644 --- a/src/main/java/com/basic/study/config/SecurityConfig.java +++ b/src/main/java/com/basic/study/config/SecurityConfig.java @@ -1,43 +1,18 @@ package com.basic.study.config; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; - -import java.util.List; @Configuration public class SecurityConfig { - @Value("${web.port}") - private String port; - @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .httpBasic(AbstractHttpConfigurer::disable) - .csrf(AbstractHttpConfigurer::disable) - .cors(cors -> cors - .configurationSource(corsConfigurationSource())); - + .csrf(AbstractHttpConfigurer::disable); return http.build(); } - - @Bean - public CorsConfigurationSource corsConfigurationSource() { - CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(List.of("http://localhost:" + port)); - configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE")); - configuration.setAllowCredentials(true); - configuration.setMaxAge(3600L); - - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", configuration); - return source; - } } diff --git a/src/main/java/com/basic/study/config/WebConfig.java b/src/main/java/com/basic/study/config/WebConfig.java new file mode 100644 index 0000000..c6896e1 --- /dev/null +++ b/src/main/java/com/basic/study/config/WebConfig.java @@ -0,0 +1,20 @@ +package com.basic.study.config; + +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; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + @Value("${web.port}") + private String port; + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/api/**") + .allowedOrigins("http://localhost:" + port) + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*"); + } +}