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 ;
6
3
import org .springframework .context .annotation .Configuration ;
7
- import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
8
4
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
5
15
6
@ Configuration
16
7
@ EnableWebSecurity
17
8
public class SecurityConfig {
18
- private final CustomOAuth2UserService customOAuth2UserService ;
19
-
20
- public SecurityConfig (CustomOAuth2UserService customOAuth2UserService ) {
21
- this .customOAuth2UserService = customOAuth2UserService ;
22
- }
23
-
24
- @ Bean
25
- protected SecurityFilterChain config (HttpSecurity http ) throws Exception {
26
- // 권한 설정
27
- http
28
- .authorizeHttpRequests (auth -> auth
29
- .requestMatchers ("/actuator/**" ).permitAll () // actuator 엔드포인트 허용
30
- .anyRequest ().authenticated () // 그 외 모든 요청은 인증 필요
31
- );
32
-
33
- //로그인 관련 설정
34
- http
35
- .oauth2Login (oauth2 -> oauth2
36
- .loginPage ("/login" ) // 로그인 페이지 지정
37
- .successHandler ((request , response , authentication ) -> {
38
- // 인증 정보가 SecurityContext에 추가되는 것을 보장
39
- SecurityContextHolder .getContext ().setAuthentication (authentication );
40
-
41
- CustomOAuth2User customUser = (CustomOAuth2User ) authentication .getPrincipal ();
42
-
43
- // 디버깅: authentication 정보 확인
44
- System .out .println ("Authentication: " + authentication );
45
- System .out .println ("Principal: " + authentication .getPrincipal ());
46
-
47
- if (authentication != null && authentication .getPrincipal () != null ) {
48
- //인가가 있으면 유저 정보를 저장
49
- OAuth2User principal = (OAuth2User ) authentication .getPrincipal ();
50
- String username = principal .getAttribute ("login" );
51
-
52
- // 세션에 사용자 정보를 추가
53
- request .getSession ().setAttribute ("user" , username );
54
-
55
- response .sendRedirect ("/" ); // 로그인 성공 후 리다이렉트
56
- } else {
57
- // 인증 실패 시 처리
58
- response .sendRedirect ("/login?error=authenticationFailed" );
59
- }
60
- })
61
- )
62
- .sessionManagement (session -> session
63
- .sessionCreationPolicy (SessionCreationPolicy .IF_REQUIRED ) // 세션 정책 설정
64
- .invalidSessionUrl ("/login?error=invalidSession" ) // 세션이 유효하지 않으면 이동할 URL
65
- .maximumSessions (1 ) // 하나의 계정으로 한 번에 로그인할 수 있도록 제한
66
- .expiredUrl ("/login?error=sessionExpired" ) // 세션 만료 후 이동할 URL 설정
67
- );
68
-
69
- //로그아웃 관련 설정
70
- http
71
- .logout (logout -> logout
72
- .logoutUrl ("/logout" ) // 로그아웃 URL 설정
73
- .logoutSuccessUrl ("/" ) // 로그아웃 성공 후 이동할 URL
74
- .invalidateHttpSession (true ) // 로그아웃 시 세션 무효화
75
- .clearAuthentication (true ) // 인증 정보 지우기
76
- .deleteCookies ("JSESSIONID" ) // 세션 쿠키 삭제
77
- );
78
- http
79
- .csrf (
80
- AbstractHttpConfigurer ::disable // CSRF 보호 비활성화
81
- );
82
-
83
- return http .build ();
84
- }
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
+ // }
85
76
}
0 commit comments