Spring Security의 인증 처리 흐름

grapefruit·2022년 11월 21일
0

BE 2022-11.21~11.25

목록 보기
1/2

Spring Security의 컴포넌트로 보는 인증(Authentication) 처리 흐름

  1. 로그인(uesrname, password) http requset 요청이 들어온다.
    (usernamepassword)AuthenticationFilter가 해당 요청을 전달 받는다.

  2. (UsernamePassword)AuthenticationFilter는 Username과 Password를 이용해 UsernamePasswordAuthenticationToken 을 생성한다.

  1. UsernamePasswordAuthenticationTokenAuthentication 인터페이스를 구현한 구현 클래스이며,
    ※주의점※
    여기서의 Authentication은 아직 인증이 되지 않은 Authentication이라는 사실을 기억한다.

  2. 아직 인증되지 않은 Authentication을 가지고있는 (UsernamePassword)AuthenticationFilter는 해당 AuthenticationAuthenticationManager에게 전달한다.

  3. AuthenticationManager로 부터 Authentication을 전달 받는다.

  4. ProviderManager로부터 Authentication을 전달 받은 AuthenticationProviderUserDetailsService를 이용해 UserDetails를 조회한다.

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

  6. UserDetails를 전달 받은 AuthenticationProviderPasswordEncoder를 이용해
    UserDetails에 포함된 암호화 된 Password와
    인증을 위한 Authentication안에 포함된 Password가 일치하는지 검증한다.

  • 검증에 성공하면 UserDetails를 이용해 인증된 Authentication을 생성한다.
  • 만약 검증에 성공하지 못하면 Exception을 발생시키고 인증 처리를 중단한다.

AuthenticationProvider는 인증된 AuthenticationProviderManager에게 전달한다.

3.에서의 Authentication은 인증을 위해 필요한 사용자의 로그인 정보를 가지고 있지만,
8.에서의 단계에서 ProviderManager에게 전달한 Authentication은 인증에 성공한 사용자의 정보(Principal, Credential, GrantedAuthorities)를 가지고 있다는 사실을 꼭 기억해야한다.

  1. 이제 ProviderManager는 인증된 Authentication을 다시 (UsernamePassword)AuthenticationFilter에게 전달한다.

  2. 인증된 Authentication을 전달 받은 (UsernamePassword)AuthenticationFilter는 마지막으로 SecurityContextHolder를 이용해 SecurityContext에 인증된 Authentication을 저장한다.

profile
개발자몽

0개의 댓글