1
1
package cmf .commitField .global .security ;
2
2
3
+ import cmf .commitField .domain .user .entity .CustomOAuth2User ;
4
+ import cmf .commitField .domain .user .service .CustomOAuth2UserService ;
5
+ import org .springframework .context .annotation .Bean ;
3
6
import org .springframework .context .annotation .Configuration ;
7
+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
4
8
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
9
+ import org .springframework .security .config .annotation .web .configurers .AbstractHttpConfigurer ;
10
+ import org .springframework .security .config .http .SessionCreationPolicy ;
11
+ import org .springframework .security .core .context .SecurityContextHolder ;
12
+ import org .springframework .security .oauth2 .core .user .OAuth2User ;
13
+ import org .springframework .security .web .SecurityFilterChain ;
14
+ import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
15
+
16
+ import static org .springframework .security .config .Customizer .withDefaults ;
5
17
6
18
@ Configuration
7
19
@ EnableWebSecurity
8
20
public class SecurityConfig {
9
- // private final CustomOAuth2UserService customOAuth2UserService;
10
- //
11
- // public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
12
- // this.customOAuth2UserService = customOAuth2UserService;
13
- // }
14
- //
15
- // @Bean
16
- // protected SecurityFilterChain config(HttpSecurity http) throws Exception {
17
- // // ๊ถํ ์ค์
18
- // http
19
- // .authorizeHttpRequests(auth -> auth
20
- // .requestMatchers("/actuator/**").permitAll() // actuator ์๋ํฌ์ธํธ ํ์ฉ
21
- // .anyRequest().authenticated() // ๊ทธ ์ธ ๋ชจ๋ ์์ฒญ์ ์ธ์ฆ ํ์
22
- // );
23
- //
24
- // //๋ก๊ทธ์ธ ๊ด๋ จ ์ค์
25
- // http
26
- // .oauth2Login(oauth2 -> oauth2
27
- // .loginPage("/login") // ๋ก๊ทธ์ธ ํ์ด์ง ์ง์
28
- // .successHandler((request, response, authentication) -> {
29
- // // ์ธ์ฆ ์ ๋ณด๊ฐ SecurityContext์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ณด์ฅ
30
- // SecurityContextHolder.getContext().setAuthentication(authentication);
31
- //
32
- // CustomOAuth2User customUser = (CustomOAuth2User) authentication.getPrincipal();
33
- //
34
- // // ๋๋ฒ๊น
: authentication ์ ๋ณด ํ์ธ
35
- // System.out.println("Authentication: " + authentication);
36
- // System.out.println("Principal: " + authentication.getPrincipal());
37
- //
38
- // if (authentication != null && authentication.getPrincipal() != null) {
39
- // //์ธ๊ฐ๊ฐ ์์ผ๋ฉด ์ ์ ์ ๋ณด๋ฅผ ์ ์ฅ
40
- // OAuth2User principal = (OAuth2User) authentication.getPrincipal();
41
- // String username = principal.getAttribute("login");
42
- //
43
- // // ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ถ๊ฐ
44
- // request.getSession().setAttribute("user", username);
45
- //
46
- // response.sendRedirect("/"); // ๋ก๊ทธ์ธ ์ฑ๊ณต ํ ๋ฆฌ๋ค์ด๋ ํธ
47
- // } else {
48
- // // ์ธ์ฆ ์คํจ ์ ์ฒ๋ฆฌ
49
- // response.sendRedirect("/login?error=authenticationFailed");
50
- // }
51
- // })
52
- // )
53
- // .sessionManagement(session -> session
54
- // .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) // ์ธ์
์ ์ฑ
์ค์
55
- // .invalidSessionUrl("/login?error=invalidSession") // ์ธ์
์ด ์ ํจํ์ง ์์ผ๋ฉด ์ด๋ํ URL
56
- // .maximumSessions(1) // ํ๋์ ๊ณ์ ์ผ๋ก ํ ๋ฒ์ ๋ก๊ทธ์ธํ ์ ์๋๋ก ์ ํ
57
- // .expiredUrl("/login?error=sessionExpired") // ์ธ์
๋ง๋ฃ ํ ์ด๋ํ URL ์ค์
58
- // );
59
- //
60
- // //๋ก๊ทธ์์ ๊ด๋ จ ์ค์
61
- // http
62
- // .logout(logout -> logout
63
- // .logoutUrl("/logout") // ๋ก๊ทธ์์ URL ์ค์
64
- // .logoutSuccessUrl("/") // ๋ก๊ทธ์์ ์ฑ๊ณต ํ ์ด๋ํ URL
65
- // .invalidateHttpSession(true) // ๋ก๊ทธ์์ ์ ์ธ์
๋ฌดํจํ
66
- // .clearAuthentication(true) // ์ธ์ฆ ์ ๋ณด ์ง์ฐ๊ธฐ
67
- // .deleteCookies("JSESSIONID") // ์ธ์
์ฟ ํค ์ญ์
68
- // );
69
- // http
70
- // .csrf(
71
- // AbstractHttpConfigurer::disable // CSRF ๋ณดํธ ๋นํ์ฑํ
72
- // );
73
- //
74
- // return http.build();
75
- // }
21
+ private final CustomOAuth2UserService customOAuth2UserService ;
22
+
23
+ public SecurityConfig (CustomOAuth2UserService customOAuth2UserService ) {
24
+ this .customOAuth2UserService = customOAuth2UserService ;
25
+ }
26
+
27
+ @ Bean
28
+ 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
+ //๋ก๊ทธ์ธ ๊ด๋ จ ์ค์
38
+ http
39
+ .oauth2Login (oauth2 -> oauth2
40
+ .loginPage ("/login" ) // ๋ก๊ทธ์ธ ํ์ด์ง ์ง์
41
+ .successHandler ((request , response , authentication ) -> {
42
+ // ์ธ์ฆ ์ ๋ณด๊ฐ SecurityContext์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ณด์ฅ
43
+ SecurityContextHolder .getContext ().setAuthentication (authentication );
44
+
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" );
55
+
56
+ // ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ถ๊ฐ
57
+ request .getSession ().setAttribute ("user" , username );
58
+
59
+ response .sendRedirect ("/" ); // ๋ก๊ทธ์ธ ์ฑ๊ณต ํ ๋ฆฌ๋ค์ด๋ ํธ
60
+ } else {
61
+ // ์ธ์ฆ ์คํจ ์ ์ฒ๋ฆฌ
62
+ response .sendRedirect ("/login?error=authenticationFailed" );
63
+ }
64
+ })
65
+ )
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
75
+ .logout (logout -> logout
76
+ .logoutUrl ("/logout" ) // ๋ก๊ทธ์์ URL ์ค์
77
+ .logoutSuccessUrl ("/" ) // ๋ก๊ทธ์์ ์ฑ๊ณต ํ ์ด๋ํ URL
78
+ .invalidateHttpSession (true ) // ๋ก๊ทธ์์ ์ ์ธ์
๋ฌดํจํ
79
+ .clearAuthentication (true ) // ์ธ์ฆ ์ ๋ณด ์ง์ฐ๊ธฐ
80
+ .deleteCookies ("JSESSIONID" ) // ์ธ์
์ฟ ํค ์ญ์
81
+ );
82
+ http
83
+ .csrf (
84
+ AbstractHttpConfigurer ::disable // CSRF ๋ณดํธ ๋นํ์ฑํ
85
+ );
86
+
87
+ return http .build ();
88
+ }
76
89
}
0 commit comments