HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🌚
[New] 우기팀
/현지/
9주차 Authentication

9주차 Authentication

인증(Authentication) 처리

  • 사용자가 주장하는 본인이 맞는지 확인하는 절차

전체적인 흐름도

 
notion image
 

SecurityContextPersistenceFilter

  • SecurityContextRepository 인터페이스 구현체를 통해 사용자의 SecurityContext를 가져오거나 갱신함
    • 인증 관련 필터 중 가장 최상단에 위치 — 이미 인증된 사용자는 다시 로그인할 필요가 없음
    • SecurityContext가 존재하지 않는다면, empty SecurityContext를 생성함
  • SecurityContextRepository 인터페이스 기본 구현은 Session을 이용하는 HttpSessionSecurityContextRepository 클래스
notion image

동작 시나리오

익명 사용자가 접근 할 경우

  • 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder에 저장한다.
  • AnonymousAuthenticationFilter에서 AnonymousAuthenticationToken 객체를 SecurityContext에 저장한다.

인증 전 사용자가 접근 할 경우

  • 새로운 SecurityContext 객체를 생성하여 SecurityContextHolder에 저장한다.
  • UsernamePasswordAuthenticationFilter에서 인증 성공 후 UsernamePasswordAuthenticationToken 객체를 SecurityContext에 저장한다.
  • 인증이 최종 완료되면 SecurityContextPersistenceFilter가 Session에 SecurityContext를 저장한다.

인증된 사용자가 접근 할 경우

  • Session에서 SecurityContext 객체를 꺼내서 SecurityContextHolder에 저장한다.
  • SecurityContext 안에는 Authentication 객체가 존재하며 계속 인증을 유지한다.

최종 응답 시 공통 로직

  • SecurityContextHolder.clearContext()를 통해서 SecurityContext를 삭제 처리한다.
    • why? 모든 경우에 SecurityContext 객체를 SecurityContextHolder에 저장하기 때문에
    •  

AbstractAuthenticationProcessingFilter (a.k.a UsernamePasswordAuthenticationFilter)

notion image
notion image

AuthenticationManager(ProviderManager)

notion image
  • AuthenticationProvider 목록 중에서 인증 처리 요건에 맞는 AuthenticationProvider를 찾아 인증 처리를 위임한다.
  • 부모 ProviderManager를 설정하여 AuthenticationProvider를 계속 탐색 할 수 있다.
    • notion image
Spring Security Architecture
This guide is a primer for Spring Security, offering insight into the design and basic building blocks of the framework. We cover only the very basics of application security. However, in doing so, we can clear up some of the confusion experienced by developers who use Spring Security.
Spring Security Architecture
https://spring.io/guides/topicals/spring-security-architecture

SecurityContextHolder, SecurityContext, Authentication

https://docs.spring.io/spring-security/site/docs/current/reference/html5/#servlet-authentication-securitycontextholder

SecurityContextHolder

  • SecurityContext 객체 저장 방식
    • MODE_THREADLOCAL : 스레드당 SecurityContext 객체를 할당, 기본값
    • MODE_INHERITABLETHREADLOCAL : 메인 스레드와 자식 스레드에 관하여 동일한 SecurityContext 를 유지
    • MODE_GLOBAL : 응용 프로그램에서 단 하나의 SecurityContext를 저장한다
  • SecurityContextHolder.clearContext() : SecurityContext 기존 정보 초기화

SecurityContext

  • Authentication 객체가 저장되는 보관소, 언제든지 Authentication 객체를 꺼내어 쓸 수 있도록 제공되는 클래스
  • ThreadLocal에 저장되어 아무 곳에서나 참조가 가능하도록 설계
  • 인증이 완료되면 HttpSession에 “SPRING_SECURITY_CONTEXT”라는 이름으로 저장되어 어플리케이션 전반에 걸쳐 전역적인 참조 가능

Authentication

  • Principal : 인증 주체
    • 사용자의 인증 완료 여부에 따라 Principal 값이 달라짐
      • 로그인 전 Principal — 로그인 아이디 (String)
      • 로그인 후 Principal — org.springframework.security.core.userdetails.User 객체
  • Credentials : 사용자 비밀번호
  • Authorities : 인증된 사용자의 GrantedAuthority 목록
  • details : 인증 부가 정보
  • Authenticated : 인증 여부
  • 사용자를 표현하는 인증 토큰 인터페이스
    • AnonymousAuthenticationToken 클래스는 익명 사용자를 표현하기 위한 Authentication 인터페이스 구현체
    • UsernamePasswordAuthenticationToken 클래스는 로그인 아이디/비밀번호 기반 Authentication 인터페이스 구현체
    • RememberMeAuthenticationToken 클래스는 remember-me 기반 Authentication 인터페이스 구현체
  • 인증이 완료되거나 혹은 인증되지 사용자를 모두를 포괄적으로 표현하며, 인증 여부를 확인할 수 있음