티스토리 뷰

728x90

SecurityContextPersistenceFilter

  • SecurityContext 객체의 생성과 저장 조회를 하는 역할을 한다.
  • FilterChainProxy 에서 두 번째에 위치하고 있다. 나머지 필터들이 참조해서 사용하기 때문이다!
  • 익명 사용자
    • 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder에 저장
    • AnonymousAuthenticationFilter 에서 AnonymousAuthenticationToken 객체를 SecurityContext에 저장
  • 인증 시
    • 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder에 저장
    • UsernamePasswordAuthenticationFilter 에서 인증 성공 후 SecurityContext에 UsernamePasswordAuthenticationToken 객체를 SecurityContext에 저장
    • 인증이 최종 완료되면 Session 에 SecurityContext를 저장 (SecurityContextPersistenceFilter이 이 역할을 한다.)
  • 인증 후
    • Session 에서 SecurityContext 꺼내어 SecurityContextHolder 에서 저장
    • SecurityContext 안에 Authentication 객체가 존재하면 계속 인증을 유지한다.
  • 최종 응답 시 공통
    • SecurityContextHolder.clearContext()
    • 클라이언트로 응답하기 전 securitycontext를 비운다. 다만 세션에 저장해 놓고 clear 된다.

익명 사용자나 인증 시 에는 생성하여 저장하고 인증 후에는 꺼내서 저장한다는 차이점이 있다.

 

1) 매 요청마다 SecurityContextPersistenceFilter를 거치게 된다. (= 처리한다)

2) 내부적으로 HttpSecurityContextRepository 클래스가 있다. SecurityContext를 조회하고 생성하는 역할을 한다.

3) 인증 전이면 새로운 컨텍스트를 생성한다. 이때 Authentication 객체는 null이다. (아무런 인증을 받은게 없기 때문이다.)

4) 인증을 시도하는 단계로 인증 필터가 인증을 처리하게 된다.

5) 인증이 성공하게 되면 SecurityContextHolder 안의  SecurityContext 객체 안에 인증이 성공한 결과를 저장한다.

6) 계속적으로 다음 filter로 이동한 후 처리하고 클라이언트에게 응답하는 시점에 Session에 저장을 한다.

 

7) 인증 전이 아니라면! 인증을 받은 이후이기 때문에 Session에 저장되어 있는 SecurityContext를 꺼내와서 SecurityContextHolder에 저장을 한다.

 

두 가지 경우 요약

인증을 받기 전과 인증을 받은 후

인증 전에는 SecurityContext안의 Authentication 객체가 null이다. 이후 위의 인증 로직을 거치고 난 후에는 SecurityContext 안에는 인증 객체인 Authentication이 저장되어있는 걸 확인할 수 있다.

위 역할을 SecurityContextPersistenceFilter 하는 것이다.

 

참고

https://www.inflearn.com/course/%EC%BD%94%EC%96%B4-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0

 

스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security - 인프런 | 강의

초급에서 중.고급에 이르기까지 스프링 시큐리티의 기본 개념부터 API 사용법과 내부 아키텍처를 학습하게 되고 이를 바탕으로 실전 프로젝트를 완성해 나감으로써 스프링 시큐리티의 인증과

www.inflearn.com

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함