spring security piece

임기준·2025년 11월 18일

✅ Spring Security 지식 조각

AuthenticationProvider의 역할

  • AuthenticationProvider는 supports(Class<?> authentication) 메서드를 통해 자신이 처리할 수 있는 Authentication 타입을 식별한다.
  • 즉, Provider는 특정 Authentication 구현체와 쌍(pair)을 이루어 인증을 수행한다.

AuthenticationManager의 역할 및 동작 순서

  • AuthenticationManager는 여러 개의 AuthenticationProvider를 등록할 수 있으며, 일반적으로 ProviderManager 구현체가 이를 관리한다.
  • 등록된 순서대로 각 Provider에 인증을 위임하고, 첫 번째로 인증에 성공한 Provider가 반환한 Authentication 객체를 그대로 리턴하며 이후 Provider는 호출되지 않는다.
  • 모든 Provider가 인증을 실패하면 최종적으로 AuthenticationException을 던진다.

에러 처리 흐름

  • 예외 발생 흐름은 다음과 같다:
    AuthenticationProvider → AuthenticationManager → ExceptionTranslationFilter

  • ExceptionTranslationFilter는 발생한 AuthenticationException을 감지하여, 인증되지 않은 사용자의 경우 로그인 페이지로 리다이렉트하거나 401 응답을 반환한다.

UserDetails와 Authentication 객체의 관계

  • UserDetails는 주로 DB 기반의 사용자 정보(아이디, 비밀번호, 권한 등)를 표현한다.
  • Authentication 객체는 인증 상태 및 Principal(즉, UserDetails), 자격 증명(Credentials), 권한(Authorities)을 포함한다.
  • 인증이 완료되면 SecurityContext에 저장되어 SecurityContextHolder를 통해 전역 접근이 가능하다.

Authentication 객체의 구성 및 인증 상태

  • Authentication 인터페이스는 getPrincipal(), getCredentials(), getAuthorities(), isAuthenticated() 등의 메서드를 가진다.
  • AbstractAuthenticationToken은 이를 구현한 추상 클래스이며, 인증 여부를 제어하는 setAuthenticated(boolean) 메서드를 제공한다.
  • 생성자에 따라 인증 상태가 구분되기도 하는데, 예를 들어:
new UsernamePasswordAuthenticationToken(principal, credentials) → 인증 전(isAuthenticated=false)
new UsernamePasswordAuthenticationToken(principal, credentials, authorities) → 인증 완료(isAuthenticated=true)

Authentication 구현체의 다양성

  • Authentication 인터페이스를 구현한 클래스는 매우 다양하다. 예시:
  • 기본 인증 관련: UsernamePasswordAuthenticationToken, RememberMeAuthenticationToken, AnonymousAuthenticationToken, PreAuthenticatedAuthenticationToken
  • OAuth2 / JWT 관련: OAuth2AuthenticationToken, JwtAuthenticationToken, BearerTokenAuthentication, OAuth2LoginAuthenticationToken
  • SAML / CAS / WebAuthn 등: Saml2Authentication, CasAuthenticationToken, WebAuthnAuthentication
    이러한 다양한 토큰 타입만큼, 각각에 대응하는 AuthenticationProvider 구현체도 존재한다.

관련이미지

기본구조

profile
openerzone

0개의 댓글