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 .security .web .util .matcher .AntPathRequestMatcher ;
14+ import org .springframework .web .cors .CorsConfiguration ;
15+ import org .springframework .web .cors .CorsConfigurationSource ;
16+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
1517
16- import static org . springframework . security . config . Customizer . withDefaults ;
18+ import java . util . List ;
1719
1820@ Configuration
1921@ EnableWebSecurity
@@ -26,64 +28,56 @@ public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
2628
2729 @ Bean
2830 protected SecurityFilterChain config (HttpSecurity http ) throws Exception {
29- // ๊ถํ ์ค์
30- http
31- .cors (withDefaults ()) // CORS ์ค์ ํ์ฑํ
32- .csrf (csrf -> csrf .disable ())
33- .authorizeHttpRequests ((authorizeHttpRequests ) -> authorizeHttpRequests
34- .requestMatchers (new AntPathRequestMatcher ("/**" )).permitAll ())
35- ;
36-
37- //๋ก๊ทธ์ธ ๊ด๋ จ ์ค์
3831 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+ )
3940 .oauth2Login (oauth2 -> oauth2
4041 .loginPage ("/login" ) // ๋ก๊ทธ์ธ ํ์ด์ง ์ง์
4142 .successHandler ((request , response , authentication ) -> {
42- // ์ธ์ฆ ์ ๋ณด๊ฐ SecurityContext์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ณด์ฅ
4343 SecurityContextHolder .getContext ().setAuthentication (authentication );
4444
45- CustomOAuth2User customUser = (CustomOAuth2User ) authentication .getPrincipal ();
46-
47- // ๋๋ฒ๊น
: authentication ์ ๋ณด ํ์ธ
48- System .out .println ("Authentication: " + authentication );
49- System .out .println ("Principal: " + authentication .getPrincipal ());
50-
51- if (authentication != null && authentication .getPrincipal () != null ) {
52- //์ธ๊ฐ๊ฐ ์์ผ๋ฉด ์ ์ ์ ๋ณด๋ฅผ ์ ์ฅ
53- OAuth2User principal = (OAuth2User ) authentication .getPrincipal ();
54- String username = principal .getAttribute ("login" );
45+ OAuth2User principal = (OAuth2User ) authentication .getPrincipal ();
46+ String username = principal .getAttribute ("login" );
5547
56- // ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ถ๊ฐ
57- request . getSession (). setAttribute ( "user" , username );
48+ // ๋๋ฒ๊น
๋ก๊ทธ
49+ System . out . println ( "OAuth2 ๋ก๊ทธ์ธ ์ฑ๊ณต: " + username );
5850
59- response .sendRedirect ("/" ); // ๋ก๊ทธ์ธ ์ฑ๊ณต ํ ๋ฆฌ๋ค์ด๋ ํธ
60- } else {
61- // ์ธ์ฆ ์คํจ ์ ์ฒ๋ฆฌ
62- response .sendRedirect ("/login?error=authenticationFailed" );
63- }
51+ response .sendRedirect ("http://localhost:5173/home" ); // ๋ก๊ทธ์ธ ์ฑ๊ณต ํ ๋ฆฌ๋ค์ด๋ ํธ
6452 })
6553 )
66- .sessionManagement (session -> session
67- .sessionCreationPolicy (SessionCreationPolicy .IF_REQUIRED ) // ์ธ์
์ ์ฑ
์ค์
68- .invalidSessionUrl ("/login?error=invalidSession" ) // ์ธ์
์ด ์ ํจํ์ง ์์ผ๋ฉด ์ด๋ํ URL
69- .maximumSessions (1 ) // ํ๋์ ๊ณ์ ์ผ๋ก ํ ๋ฒ์ ๋ก๊ทธ์ธํ ์ ์๋๋ก ์ ํ
70- .expiredUrl ("/login?error=sessionExpired" ) // ์ธ์
๋ง๋ฃ ํ ์ด๋ํ URL ์ค์
71- );
72-
73- //๋ก๊ทธ์์ ๊ด๋ จ ์ค์
74- http
7554 .logout (logout -> logout
76- .logoutUrl ("/logout" ) // ๋ก๊ทธ์์ URL ์ค์
77- .logoutSuccessUrl ("/" ) // ๋ก๊ทธ์์ ์ฑ๊ณต ํ ์ด๋ํ URL
55+ .logoutUrl ("api/logout" ) // ๋ก๊ทธ์์ URL ์ค์
7856 .invalidateHttpSession (true ) // ๋ก๊ทธ์์ ์ ์ธ์
๋ฌดํจํ
7957 .clearAuthentication (true ) // ์ธ์ฆ ์ ๋ณด ์ง์ฐ๊ธฐ
8058 .deleteCookies ("JSESSIONID" ) // ์ธ์
์ฟ ํค ์ญ์
81- );
82- http
83- .csrf (
84- 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+ })
8564 );
8665
8766 return http .build ();
8867 }
89- }
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