Skip to content

Commit

Permalink
[#64] fix: cors 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonstrup committed Feb 6, 2025
1 parent 5374e48 commit a3506e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.nexters.jaknaesoserver.config;

import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

private static final String LOCAL_URL = "http://localhost:3000";
private static final String JAKNAESO_WEB_VERCEL_APP = "https://jaknaeso-web.vercel.app";

@Value("${cors.origins.api-doc}")
private String API_DOC_HOST;

@Bean
public CorsConfigurationSource corsConfigurationSource() {

final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedOrigins(List.of(API_DOC_HOST, LOCAL_URL, JAKNAESO_WEB_VERCEL_APP));
corsConfiguration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE"));
corsConfiguration.addAllowedHeader("*");
corsConfiguration.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfigurationSource;

@RequiredArgsConstructor
@EnableWebSecurity
Expand All @@ -20,6 +21,7 @@ public class SecurityConfig {

private final SecurityExceptionHandler securityExceptionHandler;
private final JwtAuthFilter jwtAuthFilter;
private final CorsConfigurationSource corsConfigurationSource;

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand All @@ -32,6 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.permitAll()
.anyRequest()
.authenticated())
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource))
.formLogin(AbstractHttpConfigurer::disable)
.logout(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
Expand Down

This file was deleted.

0 comments on commit a3506e1

Please sign in to comment.