11package cmf .commitField .global .security ;
22
3- import cmf .commitField .domain .user .entity .CustomOAuth2User ;
43import cmf .commitField .domain .user .service .CustomOAuth2UserService ;
4+ import jakarta .servlet .http .HttpServletResponse ;
55import org .springframework .context .annotation .Bean ;
66import org .springframework .context .annotation .Configuration ;
77import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1111import org .springframework .security .core .context .SecurityContextHolder ;
1212import org .springframework .security .oauth2 .core .user .OAuth2User ;
1313import org .springframework .security .web .SecurityFilterChain ;
14+ import org .springframework .web .cors .CorsConfiguration ;
15+ import org .springframework .web .cors .CorsConfigurationSource ;
16+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
17+
18+ import java .util .List ;
1419
1520@ Configuration
1621@ EnableWebSecurity
@@ -23,56 +28,56 @@ public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
2328
2429 @ Bean
2530 protected SecurityFilterChain config (HttpSecurity http ) throws Exception {
26- //로그인 관련 설정
2731 http
32+ .cors (cors -> cors .configurationSource (corsConfigurationSource ())) // CORS 설정 추가
33+ .csrf (AbstractHttpConfigurer ::disable ) // CSRF 보호 비활성화
34+ .sessionManagement (session -> session
35+ .sessionCreationPolicy (SessionCreationPolicy .IF_REQUIRED ) // 세션 정책 설정
36+ .invalidSessionUrl ("/login?error=invalidSession" ) // 세션이 유효하지 않으면 이동할 URL
37+ .maximumSessions (1 ) // 하나의 계정으로 한 번에 로그인할 수 있도록 제한
38+ .expiredUrl ("/login?error=sessionExpired" ) // 세션 만료 후 이동할 URL 설정
39+ )
2840 .oauth2Login (oauth2 -> oauth2
2941 .loginPage ("/login" ) // 로그인 페이지 지정
3042 .successHandler ((request , response , authentication ) -> {
31- // 인증 정보가 SecurityContext에 추가되는 것을 보장
3243 SecurityContextHolder .getContext ().setAuthentication (authentication );
3344
34- CustomOAuth2User customUser = (CustomOAuth2User ) authentication .getPrincipal ();
45+ OAuth2User principal = (OAuth2User ) authentication .getPrincipal ();
46+ String username = principal .getAttribute ("login" );
3547
36- // 디버깅: authentication 정보 확인
37- System .out .println ("Authentication: " + authentication );
38- System .out .println ("Principal: " + authentication .getPrincipal ());
48+ // 디버깅 로그
49+ System .out .println ("OAuth2 로그인 성공: " + username );
3950
40- if (authentication != null && authentication .getPrincipal () != null ) {
41- //인가가 있으면 유저 정보를 저장
42- OAuth2User principal = (OAuth2User ) authentication .getPrincipal ();
43- String username = principal .getAttribute ("login" );
44-
45- // 세션에 사용자 정보를 추가
46- request .getSession ().setAttribute ("user" , username );
47-
48- response .sendRedirect ("/" ); // 로그인 성공 후 리다이렉트
49- } else {
50- // 인증 실패 시 처리
51- response .sendRedirect ("/login?error=authenticationFailed" );
52- }
51+ response .sendRedirect ("http://localhost:5173/home" ); // 로그인 성공 후 리다이렉트
5352 })
5453 )
55- .sessionManagement (session -> session
56- .sessionCreationPolicy (SessionCreationPolicy .IF_REQUIRED ) // 세션 정책 설정
57- .invalidSessionUrl ("/login?error=invalidSession" ) // 세션이 유효하지 않으면 이동할 URL
58- .maximumSessions (1 ) // 하나의 계정으로 한 번에 로그인할 수 있도록 제한
59- .expiredUrl ("/login?error=sessionExpired" ) // 세션 만료 후 이동할 URL 설정
60- );
61-
62- //로그아웃 관련 설정
63- http
6454 .logout (logout -> logout
65- .logoutUrl ("/logout" ) // 로그아웃 URL 설정
66- .logoutSuccessUrl ("/" ) // 로그아웃 성공 후 이동할 URL
55+ .logoutUrl ("api/logout" ) // 로그아웃 URL 설정
6756 .invalidateHttpSession (true ) // 로그아웃 시 세션 무효화
6857 .clearAuthentication (true ) // 인증 정보 지우기
6958 .deleteCookies ("JSESSIONID" ) // 세션 쿠키 삭제
70- );
71- http
72- .csrf (
73- AbstractHttpConfigurer ::disable // CSRF 보호 비활성화
59+ .logoutSuccessHandler ((request , response , authentication ) -> {
60+ System .out .println ("로그아웃 성공" );
61+ response .setStatus (HttpServletResponse .SC_OK );
62+ response .sendRedirect ("http://localhost:5173/" ); // 로그아웃 후 홈으로 이동
63+ })
7464 );
7565
7666 return http .build ();
7767 }
78- }
68+
69+ @ Bean
70+ public CorsConfigurationSource corsConfigurationSource () {
71+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
72+ CorsConfiguration config = new CorsConfiguration ();
73+ config .setAllowCredentials (true );
74+
75+ // setAllowedOrigins 대신 setAllowedOriginPatterns 사용
76+ config .setAllowedOrigins (List .of ("http://localhost:5173/" ));
77+
78+ config .setAllowedMethods (List .of ("GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ));
79+ config .setAllowedHeaders (List .of ("Authorization" , "Content-Type" ));
80+ source .registerCorsConfiguration ("/**" , config );
81+ return source ;
82+ }
83+ }
0 commit comments