본문 바로가기

Language/Spring Security

기억하기 인증 필터 - RememberMeAuthenticationFilter

  • SecurityContextHolder에 Authentication이 포함되지 않은 경우 실행되는 필터
    • 인증 정보가 SecurityContextHolder에 존재한다는 것은 인증이 유지되고 있다는 의미로, 인증 정보가 없을 때 RememberMeAuthenticationFilter를 실행
  • 세션이 만료되었거나 어플리케이션 종료로 인해 인증 상태가 소멸된 경우 토큰 기반 인증을 사용해 유효성을 검사하고 토큰이 검증되면 자동 로그인 처리를 수행
    • 이전 글에서 토큰이 있는 채로 JSESSIONID를 삭제하고 다시 접속하였을 때 로그인 페이지로 튕겨지지 않은 것

 

기억하기 인증 필터 흐름도

  1. Client가 GET 방식으로 “/user” URL 요청
  2. RememberMeAuthenticationFilter 실행
  3. Authentication이 null인지 아닌 지 확인
    • null이면 자동로그인을 할 수 있도록 다음으로 스텝으로 진행
    • null이 아니면 인증 상태이기 때문에 패스 → chain.doFilter()
  4. RememberMeService.autoLogin() 호출
  5. RememberMeAuthenticationToken에 사용자 정보, 권한 정보를 담음
    • Form인증에서는 username과 password를 토큰에 담음
  6. AuthenticationManager에게 토큰 전달 → 인증 성공 or 인증 실패

인증 성공

  1. RememberMeAuthenticationToken 전달
  2. SecurityContextHolder → Authentication을 SecurityContext에 설정
  3. SecurityContextRepository → 세션에 SecurityContext 저장
  4. ApplicationEventPublisher → 인증 성공 이벤트 게시

인증 실패

  1. RememberMeServices
  2. loginFail() → remember-me 쿠키 삭제

정리

  • SecurityContextHolder에 인증 정보가 없을(null) 경우 RememberMeServices의 autoLogin() 실행
  • 폼 인증에서의 UsernamePasswordAuthenticationToken에서는 username, password를 담아 AuthenticationManager에게 전달하였지만, 기억하기에서는 RememberMeAuthenticationToken에 UserDetails(유저 정보) + Authorities(권한)을 담아서 전달
  • 세션에 저장하여 자동 로그인이 될 수 있도록 함

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

로그 아웃 -­ logout()  (0) 2024.07.16
익명 사용자 ­- anonymous()  (0) 2024.07.16
기억하기 인증 - rememberMe()  (0) 2024.07.16
기본 인증 필터 - BasicAuthenticationFilter  (0) 2024.07.16
기본 인증 - httpBasic()  (0) 2024.07.16