Spring Security
- 스프링 기반 애플리케이션의 보안(인증, 권한, 인가 등) 설정을 도와주는 하위 프레임워크
동작 구조
- 스프링 애플리케이션과의 의존성 분리를 위해 서블릿 필터를 기반으로 동작
- Spring Security는 서블릿 필터 중 한 개로 등록되어 보안 설정을 수행
- DelegatingFilterProxy 인터페이스에서 Spring Security 필터를 구현하며 이 필터는 여러 개의 체인으로 구성 => SecurityFileterChain
- SecurityFileter = SecurityFileterChain으로 관리되는 필터체인으로 각 필터마다 보안과 관련된 고유한 기능을 담당
ex) 로그인 담당 / 세션 담당 / 로그아웃 담당 등
동작 순서
1. 사용자가 Form을 통하여 로그인 정보를 담아 HttpRequest 요청
2. AuthenticationFilter가 요청을 받아 UsernamePasswordAuthenticationToken 생성
※ UsernamePasswordAuthenticationToken => 해당 요청을 처리할 수 있는 Provider 탐색에 사용
3. AuthenticationFilter가 AuthenticationManager에게 UsernamePasswordAuthenticationToken 전달
4. AuthenticationManager가 Token을 처리할 수 있는 AuthenticationProvider 탐색
5. 선택된 AuthenticationProvider 실행 => DB와 로그인 정보 비교
6&7. DB와 로그인 정보 비교를 위해 UserDetailsService에서 loadUserByUsername 메소드 수행 => 일치하면 UserDetails 객체를 return, 그렇지 않으면 예외 발생
8. AuthenticationProvider가 반환된 UserDetails 객체를 Authentication 객체에 담아 AuthenticationManager에게 Authentication 객체 전달
9. AuthenticationManager가 AuthenticationFilter에게 Authentication 객체 전달
10. AuthenticationFilter가 Authentication 객체를 SecurityContextHolder에 담은 후 AuthenticationSuccessHandler 실행 (실패하면 AuthenticationFailureHandler 실행)
※ 인증된 사용자가 재요청 시 위 과정을 실행하지 않고 바로 SecurityContext에 Authentication 객체가 있는지 확인하여 있다면 바로 효율적으로 처리 가능
'Spring' 카테고리의 다른 글
의존성 주입 방식 (0) | 2023.05.25 |
---|---|
JWT (0) | 2023.05.25 |
Spring Security 기본 로그인 구현 중 실수 정리 (0) | 2023.05.11 |
Spring Security 기본 세팅 (0) | 2023.05.09 |
Thymeleaf 기본 문법 (0) | 2023.05.08 |
댓글