본문 바로가기
IT Study/SpringBoot

Spring Security 인증 구조 - 3

by Irwin-Kr 2025. 7. 12.

ProviderManager

일반적으로 AuthenticationManager의 구현에 사용되고

AuthenticationProvider 요소의 목록에 ProviderManager를 위임한다.

 

각 AuthenticationProvider는 인증의 성공, 실패, 불가를 표시하고

AuthenticationProvider 흐름에 허용을 결정하는 기회를 가진다.

 

설정된 AuthenticationProvider 요소들 중 어느것도 인증을 할 수 없으면,

ProviderNotFoundException과 함께 인증이 실패한다.

 

ProviderNotFoundException은 ProviderManager가 

전달된 인증의 유형을 지원하도록 구성하지 않았음을 나타낸다.

 

 

각 AuthenticationProvider는 인증의 특별한 유형을 수행하는 방법을 안다.

 

예를들면, 한 AuthenticationProvider는 사용자 이름/비밀번호를 검증 할 수 있고,

다른 AuthenticationProvider는 SAML assertion을 인증 할 수 있다.

 

이를 통해 각 AuthenticationProvider는

여러 유형의 인증을 지원하면서 매우 특별한 유형의 인증을 하고

단일 AuthenticationManager Bean만 나타낸다.

 

ProviderManager는 AuthenticationProvider가 인증을 수행할 수 없는 상황에

참고할 상위 AuthenticationManager로 선택적 구성이 가능하다.

 

 

 

실제로 여러 ProviderManager 요소는 같은 상위 AuthenticationManager를 공유하며,

공통 인증을 가진 여러 SecurityFilterChain 요소(공유된 상위 AuthenticationManager)와

다른 인증 매커니즘(다른 ProviderManager 요소)에도 다소 일반적이다.

 

 

기본적으로 ProviderManager는 성공적인 인증 요청에 반환되는

민감한 자격 증명 정보를 인증 객체로부터 지우려고 한다.

비밀번호와 같은 정보를 HttpSession에서 필요한 것 보다 길게 유지하는 것을 방지한다.

 

CredentialsContainer interface는 인증 과정에서 중요한 역할을 수행한다.

더 이상 필요하지 않은 자격 증명 정보를 지워

민감한 데이터가 필요 이상 저장하지 않게 보안을 향상시킨다.

 

예를 들면,  무상태 응용프로그램에서 향상된 성능을 위해

사용자 객체의 cache를 사용할 때, 문제가 발생할 수 있다.

 

인증은 캐시내 객체에 참조가 포함되고,

자격 증명이 제거되면 더이상 cache 값에 대해 인증할 수 없다.

(cache를 사용하는 경우 고려해야 한다.)

 

분명한 해결책은 cache 구현 또는 반환된 인증 객체로

생성되는 AuthenticationProvider에서 먼저 객체의 복사본을 만들거나,

ProviderManager에서  eraseCredentialsAfterAuthentication 설정을  비활성화 할 수 있다.

 

AuthenticationProvider

ProviderManager에 여러 AuthenticationProvider 요소를 주입

 

각 AuthenticationProvider는 인증의 특별한 유형을 수행한다.

 

예를 들면, DaoAuthenticationProvider는

사용자 이름/비밀번호 기반 인증을 지원하지만

JwtAuthenticationProvider는 JWT Token 인증을 지원한다.

 

AuthenticationEntryPoint와 자격 증명 요청

단말로부터 자격 증명 요청을 HTTP 응답 전송에 사용한다.

 

1️⃣

단말은  자원 요청으로 자격 증명들(사용자 이름과 비밀번호 같은)을 

주도적/능동적으로 포함한다.

 

이때, Spring Security는

이미 단말에 자격 증명이 포함되어 있을 때,

단말로 부터 자격 증명 요청을 HTTP 응답으로 제공할 필요 없다.

 

2️⃣

단말이 접근에 인정받지 않은 자원에 인증되지 않은 요청을 생성한다.

AuthenticationEntryPoint의 구현은 단말에서 자격증명 요청으로 사용된다.

 

AuthenticationEntryPoint 구현은 로그인 페이지로 이동,

WWW-Authentication header로 응답, 또는 다른 작업을 수행할 수도 있다.

 

AbstractAuthenticationProcessingFilter

사용자의 자격 증명 인증을 위한 기본 Filter로서 사용된다.

 

자격 증명 인증 전에 Spring Security는 일반적으로

AuthenticationEntryPoinit를 사용하여 자격 증명 요청한다.

 

다음으로 AbstractAuthenticationProcessingFilter는

제출된 모든 인증 요청에 인증 할 수 있다.

 

 

 

1️⃣

사용자가 자격 증명을 제출 했을떄

AbstractAuthenticationProcessingFIlter는

인증을 위해 HttpServletRequest에서 인증을 생성한다.

 

생성된 인증의 유형은

AbstractAuthenticationProcessingFIlter의

subClass에 종속된다.

 

예를 들면,

UsernamePasswordAuthenticationFilter는

HttpServletRequest에 제출된 사용자 명과 비밀번호으로

UsernamePasswordAuthenticationToken을 만든다.

 

 

2️⃣

인증은 AuthenticationManager에 넘겨져 인증된다.

 

 

3️⃣

인증에 실패할 경우, SecurityContextHolder는 비워지고,

RememberMeServices.loginFall이 호출된다. 

(※ Spring Security의 "RememberMe"을 설정하지 않으면 작동하지 않는다.)

마지막으로 AuthenticationFailureHandler가 호출된다.

 

 

4️⃣

인증에 성공할 경우,

SessionAuthenticationStrategy에 새 로그인의 통보하고,

인증은 SecurityContextHolder에 설정된다.

 

이후 SecurityContext를 저장해 향후 자동적으로 설정이 필요한 경우

SecurityContextRepository#saveContent를

명시적으로 호출해야 한다.

 

RememberMeService.loginSuccess가 호출된다.

(※ 3️⃣과 동일하게

Spring Security의 "RememberMe"을 설정하지 않으면 작동하지 않는다.)

 

ApplicationEventPublisher는 InteractiveAuthenticationSuccessEvent를 배포하고,

AuthenticationSuccessHalder 호출

 

'IT Study > SpringBoot' 카테고리의 다른 글

Spring DB Setting  (3) 2025.07.24
Spring Security 인증 구조 - 2  (0) 2025.06.28
Servlet 인증 구조  (2) 2025.06.14
Spring Security(4)  (1) 2025.05.09
Spring Security(3)  (0) 2025.04.26