Java/Spring Security

Spring Security의 인증 처리 흐름

마손리 2023. 5. 13. 17:57

이전 포스트에서 클라이언트의 요청이 Spring Security Filter까지 도달하는 과정을 포스트 했었다. 

 

이번 포스트에서는 도달한 사용자의 인증 요청이 Spring Security Filter Chain의 특정 Filter에 도달했을 때, Spring Security의 컴포넌트들이 어떤 과정을 거쳐 사용자의 인증 요청을 처리하는지 그 흐름에 대해 포스트 할 예정이다.

 

 

Spring Security의 인증(Authentication) 처리 흐름

위의 그림은 개발자가 지정한 특정한 URL(로그인)으로 포스트요청이 들어왔을때의 처리 흐름이다.

 

  • 먼저 (1)에서 사용자가 로그인 폼 등을 이용해 Username(로그인 ID)과 Password를 포함한 request를 Spring Security가 적용된 애플리케이션에 전송한다.
  • 사용자의 로그인 요청이 Spring Security의 Filter Chain까지 들어오면 여러 Filter들 중에서 UsernamePasswordAuthenticationFilter가 해당 요청을 전달받는다.

 

  • 사용자의 로그인 요청을 전달받은 UsernamePasswordAuthenticationFilter는 Username과 Password를 이용해 (2)와 같이 UsernamePasswordAuthenticationToken을 생성한다.
  • UsernamePasswordAuthenticationToken은 Authentication 인터페이스를 구현한 구현 클래스이며, 여기에서 Authentication은 아직 인증이 되지 않은 Authentication이다.

 

  • 아직 인증되지 않은 Authentication을 가지고 있는 UsernamePasswordAuthenticationFilter는 (3)과 같이 해당 Authentication을 AuthenticationManager에게 전달한다. 즉, AuthenticationManager를 구현한 ProviderManager가 인증이라는 작업을 총괄하는 실질적인 매니저이다
  • ProviderManager가 AuthenticationProvider를 찾아 인증처리를 맡긴다.
  • 즉, AuthenticationManager는 인증 처리를 총괄하는 매니저 역할을 하는 인터페이스이고, AuthenticationManager를 구현한 구현 클래스가 바로 ProviderManager이며 인증처리 작업은 실질적으로 AuthenticationProvider안에서 이뤄진다 .

 

  • (4)와 같이 ProviderManager로부터 Authentication을 전달받은 AuthenticationProvider는 (5)와 같이 UserDetailsService를 이용해 UserDetails를 조회한다. 그리고 이 UserDetails를 제공하는 컴포넌트가 바로 UserDetailsService이다.
  • UserDetails는 데이터베이스 등의 저장소에 저장된 사용자의 Username과 사용자의 자격을 증명해 주는 크리덴셜(Credential)인 Password, 그리고 사용자의 권한 정보를 포함하고 있는 컴포넌트이다.

 

  • UserDetailsService는 (5)에서 처럼 데이터베이스 등의 저장소에서 사용자의 크리덴셜(Credential)을 포함한 사용자의 정보를 조회한다.

 

  • 데이터베이스 등의 저장소에서 조회한 사용자의 크리덴셜(Credential)을 포함한 사용자의 정보를 기반으로 (7)과 같이 UserDetails를 생성한 후, 생성된 UserDetails를 다시 AuthenticationProvider에게 전달함(8).

 

  • UserDetails를 전달받은 AuthenticationProvider는 PasswordEncoder를 이용해 UserDetails에 포함된 암호화된 Password인증을 위한 Authentication안에 포함된 Password가 일치하는지 검증한다.
    • 검증에 성공하면 UserDetails를 이용해 인증된 Authentication을 생성(9).
    • 만약 검증에 성공하지 못하면 Exception을 발생시키고 인증 처리를 중단.

 

  • AuthenticationProvider는 인증된 Authentication을 ProviderManager에게 전달한다(10).
  • (2)에서의 Authentication은 인증을 위해 필요한 사용자의 로그인 정보를 가지고 있지만, 이 단계에서 ProviderManager에게 전달한 Authentication인증에 성공한 사용자의 정보(Principal, Credential, GrantedAuthorities)를 가지고 있다.

 

  • 이제 ProviderManager는 (11)과 같이 인증된 Authentication을 다시 UsernamePasswordAuthenticationFilter에게 전달한다.

 

  • 인증된 Authentication을 전달받은 UsernamePasswordAuthenticationFilter는 마지막으로 (12)와 같이 SecurityContextHolder를 이용해 SecurityContext에 인증된 Authentication을 저장한다.
  • 그리고 SecurityContext는 이후에 Spring Security의 세션 정책에 따라서 HttpSession에 저장되어 사용자의 인증 상태를 유지하기도 하고, HttpSession을 생성하지 않고 무상태를 유지하기도 한다.

'Java > Spring Security' 카테고리의 다른 글

JWT  (0) 2023.05.17
DelegatingPasswordEncoder  (0) 2023.05.15
SecurityFilterChain과 AuthenticationProvider 구현  (0) 2023.05.13
Servlet Filter 구현  (0) 2023.05.13
Spring Security Filter의 동작 흐름  (1) 2023.05.13