상세 컨텐츠

본문 제목

Spring Security Authentication 흐름

Spring Security

by Wanderer Kim 2024. 6. 9. 12:18

본문

728x90

Authentication 이란?

Authentication(인증) 이란 reousrce로 접근하려는 유저의 신원을 확인하는 방법을 의미합니다.

 

Spring Security에서 Authentication Flow

form 로그인을 했을때 인증에 대한 처리과정은 아래와 같습니다.

  • 클라이언트가 로그인을 요청한다.
  • form 인증 방식으로 요청할 때 UsernamePasswordAuthenticationFilter가 작동한다.
  • 사용자의 id와 password를 받아서 Authentication 객체를 담는다.
  • id와 password를 담은 인증 전 토큰 객체를 생성해서 AuthenticationManager에게 인증을 맡긴다.
  • AuthenticationManager가 필터로부터 받은 인증객체를 전달 받는다.
  • AuthenticationManager가 사용자의 현재 id, password가 일치하는지 여부를 확인하지 않는다. 이 클래스는 내부적으로 한 개이상의 AuthenticationProvider를 담고 잇는데 그 객체들 중에서 현재 인증을 사용될 수 있는 Provider를 찾아서 그 Provider에게 인증을 위임하는 역할을 한다.
  • AuthenticationProvider가 실제적으로 id, password와 같은 정보를 맞는지 검증하는 역할을 한다.
  • id를 전달하면서 User 객체를 전달한다.
  • 요청할 때는 UserDetailsService라는 인터페이스에게 전달하다.
  • loadUserByUsername() 호출하여 repository에서 유저 객체를 조회해서 만약 있다면 UserDetails 타입으로 반환한다.
  • 예외가 발생하면 UsernamePasswordAuthenticationFilter가 예외를 받아서 처리하게 된다.
  • AuthenticaionProvider(DaoAuthenticationProvider)는 이제 password 검증을 시작한다. 인증 객체의 password와 반환받은 UserDetails의 password를 비교한다.
  • 일치하지 않을 경우 BadCredentialException 발생 후 인증 실패처리한다.
  • 성공한 인증객체(Authentication)를 UsernamePasswordAuthenticationFilter에 전달한다.
반응형

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

Basic HTTP Authentication  (1) 2024.03.24

관련글 더보기

댓글 영역