Skip to content

Commit

Permalink
[feat] CORS 설정 추가 - #300
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Oct 12, 2024
1 parent bda9759 commit 4edc296
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dateroad.auth.config;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.dateroad.auth.exception.ExceptionHandlerFilter;
import org.dateroad.auth.exception.JwtAuthenticationEntryPoint;
Expand All @@ -14,6 +15,9 @@
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.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@RequiredArgsConstructor
@EnableWebSecurity
Expand All @@ -38,6 +42,7 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.cors(cors-> cors.configurationSource(corsConfigurationSource()))
.formLogin(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
.sessionManagement(sessionManagementConfigurer ->
Expand All @@ -55,6 +60,24 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.addFilterBefore(exceptionHandlerFilter, JwtAuthenticationFilter.class)
.build();
}
@Bean
protected CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", getDefaultCorsConfiguration());

return source;
}

private CorsConfiguration getDefaultCorsConfiguration() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOriginPatterns(List.of("*"));
configuration.setAllowedHeaders(List.of("*"));
configuration.setAllowedMethods(List.of("*"));
configuration.setAllowCredentials(true);
configuration.setMaxAge(3600L);

return configuration;
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
Expand Down

0 comments on commit 4edc296

Please sign in to comment.