Skip to content

Commit

Permalink
Merge pull request #44 from onetime-with-members/fix/#43/cors
Browse files Browse the repository at this point in the history
[fix] : CORS 문제를 해결한다
  • Loading branch information
bbbang105 authored Sep 29, 2024
2 parents 9cf7114 + 1ef5ade commit 0ff1f23
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/main/java/side/onetime/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
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 side.onetime.auth.handler.OAuthLoginFailureHandler;
import side.onetime.auth.handler.OAuthLoginSuccessHandler;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RequiredArgsConstructor
@Configuration
Expand All @@ -23,33 +24,35 @@ public class SecurityConfig {
private final OAuthLoginSuccessHandler oAuthLoginSuccessHandler;
private final OAuthLoginFailureHandler oAuthLoginFailureHandler;

// CORS 설정
@Bean
CorsConfigurationSource corsConfigurationSource() {
return request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedOrigins(List.of(
"http://localhost:5173",
"https://onetime-test.vercel.app",
"https://onetime-with-members.com"
));
config.setAllowCredentials(true);
return config;
};
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Arrays.asList(
"http://localhost:5173",
"https://onetime-test.vercel.app",
"https://www.onetime-test.vercel.app",
"https://onetime-with-members.com",
"https://www.onetime-with-members.com"
));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.
httpBasic(HttpBasicConfigurer::disable)
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource())) // CORS 설정 추가
httpSecurity
.httpBasic(HttpBasicConfigurer::disable)
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource()))
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize ->
authorize
.requestMatchers("/**").permitAll()
)

.oauth2Login(oauth -> // OAuth2 로그인 기능에 대한 여러 설정의 진입점
oauth
.successHandler(oAuthLoginSuccessHandler) // 로그인 성공 시 핸들러
Expand Down

0 comments on commit 0ff1f23

Please sign in to comment.