Skip to content

Commit

Permalink
🐛 [FIX] CORS 에러 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
ddongseop committed Jan 16, 2024
1 parent 8fd5e51 commit b03a4ee
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
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.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static io.swagger.v3.oas.models.PathItem.HttpMethod;
import static org.sopt.lequuServer.global.auth.security.AuthWhiteList.AUTH_WHITELIST;

@Configuration
Expand All @@ -22,6 +28,39 @@ public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final JwtExceptionFilter jwtExceptionFilter;

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8080", "http://localhost:5173", "http://localhost:5174", "http://localhost:3000", "http://localhost:3001", "https://lecue-client.vercel.app")
.allowedOriginPatterns("http://localhost:8080", "http://localhost:5173", "http://localhost:5174", "http://localhost:3000", "http://localhost:3001", "https://lecue-client.vercel.app")
.allowedHeaders("*")
.allowedMethods(
HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.PATCH.name()
);
}
};
}

@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");

source.registerCorsConfiguration("/v3/api-docs", config);
return new CorsFilter(source);
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
Expand Down

0 comments on commit b03a4ee

Please sign in to comment.