티스토리 뷰
Spring Security 주요 아키텍처를 알아보자.
(링크 참조 : https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html)
- SpringContextHolder : 스프링 보안이 인증된 사용자에 대한 세부 정보를 저장
- SecurityContext : SecurityContextHolder에서 가져오고 현재 인증된 사용자의 인증을 포함함.
- Authentication : 사용자가 인증을 위해 제공한 자격 증명 또는 SecurityContext의 현재 사용자를 제공하기위해 AuthenticationManager에 입력할 수 있다.
- GrantedAuthority : 인증에 대해 본인에게 부여된 권한(예 : 역할, 범위)
- AuthenticationManager : Spring Security의 필터가 인증을 수행하는 방법을 정의하는 API
- ProviderManager : 가장 일반적인 AuthenticationManager의 구현
- AuthenticationProvider : ProviderManager가 특정 유형의 인증을 수행하는 데 사용됨.
- Request Credentials with AuthenticationEntryPoint : 클라이언트로부터 자격 증명을 요청하는 데 사용됨. (예 : 로그인 페이지로 리디렉션, WWW-Authenticate 응답 전송 등)
- AbstractAuthenticationProcessingFilter : 인증에 사용되는 기본 필터 / 이를 통해 인증의 높은 수준의 흐름과 각 요소가 어떻게 작동하는지에 대한 아이디어를 얻을 수 있음.
SecurityContextHolder
- Spring Security 인증 모델의 핵심
SecurityContext 포함하고 있음.
Spring Security는 SecurityContextHolder가 어떻게 채워지는지 신경쓰지 않음.
값이 포함되어 있기만 하면 인증된 사용자로 사용됨.
Authentication
- 대부분 Spring Security 내에서 두 가지 주요 용도로 사용됨.
1. 사용자가 인증을 위해 제공한 자격 증명을 제공하기 위해 AuthenticationManager에 입력함.
이 시나리오에서 사용되는 경우 isAuthenticated()는 false 반환
2. 현재 인증된 사용자를 나타냄. Security Context에서 현재 인증을 가져올 수 있음.
Authentication에는 다음이 포함됨.
- principal : 사용자 식별
사용자 이름/비밀번호로 인증할 때는 보통 UserDetails의 인스턴스에 해당함.
- 자격증명(credentials) : 종종 비밀번호
대부분의 경우 사용자가 인증된 후에는 유출되지 않도록 지워짐.
- 권한(authorities) : GrantedAuthority 인스턴스는 사용자에게 부여된 상위 수준의 권한에 해당함.
GrantedAuthority
- 사용자에게 부여되는 상위 수준의 권한
1. Authentication.getAuthorities() 메서드에서 GrantedAuthority 인스턴스를 가져올 수 있음.
GrantedAuthority는 본인에게 부여된 권한
일반적으로 ROLE_ADMINISTRATOR 또는 ROLE_HR_SUPERVISOR와 같은 역할
사용자 이름/비밀번호 기반 인증을 사용할 때 GrantedAuthority 인스턴스는 일반적으로 UserDetailsService에 의해 로ㄷ됨.
일반적으로 GrantedAuthority 객체는 애플리케이션 전체 권한에 해당함.
따라서 많을 경우 메모리 부족이 발생할 수 있음.
AuthenticationManager
- Spring Security의 필터가 인증을 수행하는 방법을 정의하는 API
- 그런 다음 반환되는 인증은 AuthenticationManager를 호출한 컨트롤러(즉, Spring Security의 필터 인스턴스)에 의해 SecurityContextHolder에 설정됨.
- 가장 일반적인 구현은 ProviderManager
ProviderManager
- 가장 일반적으로 사용되는 AuthenticationManager
- ProviderManager는 uthenticationProvider 인스턴스 목록에 위임
- 각 Authentication Provider는 인증이 성공, 실패, 결정을 내릴 수 없음을 표시
구성된 인증 공급자 중 어느 것도 인증할 수 없는 경우, 인증은 ProviderManager가 ProviderNotFoundException과 함께 인증 실패
- 기본적으로 ProviderManager는 인증 요청에 성공하여 반환된 인증 개체에서 민감한 자격 증명 정보를 지우기 때문에
비밀번호와 같은 정보가 HttpSession에서 필요 이상으로 오래 유지되는 것을 방지할 수 있음.
AuthenticationProvider
- ProviderManager에 여러 AuthenticationProvider를 삽입할 수 있음.
Request Credentials with AuthenticationEntryPoint
- 클라이언트로부터 자격 증명을 요청하는 응답을 보내는 데 사용함.
- 때때로 클라이언트는 리소스를 요청하기 위해 사용자 이름과 비밀번호와 같은 자격 증명을 사전에 포함하기도 하는데 이러한 경우 자격 증명이 이미 포함되어 잇으므로 Spring Security는 클라이언트에서 자격 증명을 요청하는 HTTP 응답을 제공할 필요가 없음.
- 다른 경우는 클라이언트가 액세스 권한이 없는 리소스에 대해 인증되지 않은 요청을 하는 경우, 클라이언트에서 자격 증명을 요청하기 위해 AuthenticationEntryPoint 구현이 사용됨.
AuthenticationEntryPoint 구현은 로그인 페이지로 리디렉션하거나 WWW-Authenticate 헤더로 응답하거나 다른 작업을 수행할 수 있음.
AbstractAuthenticationProcessingFilter
- 사용자의 자격 증명을 인증하기 위한 기본 필터로 사용됨.
- 자격 증명을 인증하기 전에 Spring Security는 일반적으로 AuthenticationEntryPoint를 사용하여 자격 증명을 요청함.
그런 다음, AbstractAuthenticationProcessingFilter는 이 필터로 제출된 모든 인증 요청을 인증할 수 있음.
1. 사용자가 자격 증명을 제출하면 AbstractAuthenticationPRocessingFilter는 인증할 HttpServletRequest를 생성한다.
2. 다음으로 인증이 AuthenticationManager로 전달되어 인증된다.
3. 인증에 실패하면 Failure
- SecurityContextHolder는 지워짐.
- RememberMeServices.loginFail이 호출됨. (구현되지 않은 경우 실패)
- AuthenticationFailureHandler 인터페이스 참조
4. 인증 성공
- SessionAuthenticationStrategy에 새 로그인 알림을 받음.
- Authentication은 SecurityContextHolder에 설정됨.
향후 요청 시 자동으로 설정될 수 있도록 SecurityContext를 저장해야하는 경우 SecurityContextRepository#SaveContext를 명시적으로 호출해야한다.
- RememberMeServices.loginSuccess가 호출된다. (구현되지 않은 경우 실패)
- ApplicationEventPublisher는 InteractiveAuthenticationSuccessEvent를 게시한다.
- AuthenticationSuccessHandler가 호출된다.
'프로그래밍 > Spring Security' 카테고리의 다른 글
OTP 기반 2단계 인증 시스템 직접 구현해보기 - 1 시스템 아키텍처 및 3단계 인증 흐름 (0) | 2025.05.24 |
---|---|
OAuth 2와 SSO의 차이 (0) | 2025.05.23 |
SSO란? (0) | 2025.05.22 |
[Spring Security] SecurityContext의 Thread-safe 전략 (0) | 2025.05.20 |
[Spring Security in Action] 모놀리식 아키텍처 vs 마이크로서비스 시스템 (0) | 2025.05.20 |
- Total
- Today
- Yesterday
- hackerrank challenges
- MSA
- 디버깅
- 정보처리산업기사
- 22 정보처리 산업기사
- 해커랭크
- 소스코드
- 해커랭크 챌린지
- challenges
- 자바
- ORM
- 22 정보처리산업기사
- 챌린지
- 백준
- 강의
- 정보처리산업기사 공부법
- 정보처리 산업기사
- 해커랭크 자바 챌린지
- Spring Security
- queue
- 그리디
- 자바의 정석
- 이코테
- 해커랭크 자바
- 나동빈
- JPA
- hackerrank
- Java
- 코드
- BAEKJOON
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |