본문 바로가기

spring security

기본 인증 필터 - BasicAuthenticationFilter

정수원님의 **스프링 시큐리티 완전 정복** 강의 내용 기록입니다 !!

 

 

 

BasicAuthenticationFilter
  • 기본 인증 서비스 제공
  • BasicAuthenticationConverter를 사용하여 요청 헤더에 기술된 인증 정보의 유효성을 체크하고 Base64 인코딩된 username과 password를 추출
    • 유효성 체크 : 요청 헤더에 Authentication: Basic [인코딩값] 이 있는지 확인
    • 유효성 검사 성공시 인코딩값을 통해 username, password 추출하여 UsernamePasswordAuthenticationToken에 저장
  • 기본인증의 경우 일반적으로 세션을 사용하지 않음
  • 세션을 사용하는 경우 : 인증 과정에서 세션에 인증정보 저장, 매 요청마다 인증 과정을 거치지 않음
  • 세션을 사용하지 않는 경우 : 매 요청마다 인증과정을 거쳐야 함

 

흐름도

client

↓  Get  /login

** 폼 인증 필터 (UsernamePasswordAuthenticationFilter)와의 차이점

BasicAuthenticationFilter

유효성 체크 : Authentication 헤더를 포함하는지 / Basic 포함하는지
유효성 검사 성공시 : 인코딩된 값을 통해 username, password 추출

UsernamePasswordAuthenticationToken : (Username + Password) 정보를 저장 → 앞으로 인증 수행을 할 수 있도록 사용됨

AuthenticationManager : UsernamePasswordAuthenticationToken을 전달하여 token을 통해 인증 처리를 하도록 함

인증 성공 ?

Yes

UsernamePasswordAuthenticationToken : 새로 생성 (UserDetails + Authorities)
- 인증 성공했기 때문에 사용자의 username, password 뿐만 아니라 여러 사용자 정보를 가지고 있어야 함
- 보통 db에서 정보를 가져와서 객체를 생성하여 저장
- 사용자가 가진 권한도 저장
- 인증에 성공한 최종 인증 객체 !

** 폼 인증 필터 (UsernamePasswordAuthenticationFilter)와의 차이점

SecurityContextHolder : Authentication을 SecurityContext에 설정 / 요청 컨텍스트에 SecurityContext가 저장됨
- Authentication : 생성했던 최종 인증 객체인 UsernamePasswordAuthenticationToken
- 세션이 아닌 요청 컨텍스트에 저장 ≫ 요청 범위 내에(해당 요청이 끝날 때까지만) 저장됨 ≫ 다시 요청 시 매번 인증과정 필요
- 세션을 사용하는 폼로그인 방식과의 차이점 : 세션 저장 시 세션에서 받아온 정보 확인하여 만료기간까지 재요청 필요가 없지만 요청 컨텍스트에 저장하는 httpBasic 방식은 매 요청마다 인증 필요

RememberMeServices : RememberMeService.loginSuccess 호출 (Remember-me가 설정된 경우)
- 사용자 정보 기억하기 체크박스에 체크하면 ≫ Remember-me가 설정된 것
- 기억하기 인증이 활성화된 경우에 그에 대한 처리를 하는 클래스

ApplicationEventPublisher : 인증 성공 이벤트를 게시

AuthenticationSuccessHandler : 인증 성공 핸들러 호출 (API로 설정한 핸들러)

 

No

SecurityContextHolder : SecurityContextHolder가 삭제됨
- 사용자의 인증 상태를 유지하는 기능이므로, 인증 요청에 실패하면 보안을 위해 인증 상태를 삭제

RememberMeServices : RememberMeServices.loginFail 호출됨
- 자동 로그인 기능, 인증 요청에 실패하면 보안을 위해 자동 로그인 삭제

AuthenticationEntryPoint : WWW-Autenticate를 보내도록 호출됨