[docs 인덱싱] Spring Security / Servlet Applications / Authentication / Authentication Architecture

inho ha·2024년 8월 10일
0

docs 인덱싱

목록 보기
1/2

docs

https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html

SecurityContextHolder

SecurityContextHolder는 인증된 사용자 정보를 저장하는 곳이다.
Spring Security는 SecurityContextHolder의 값과 상관없이 채워져있다면 인증된 것으로 취급한다.

SecurityContextHolder는 인증된 사용자 정보를 ThreadLocal에 저장하기 때문에 메서드에 명시적으로 전달하지 않더라도, 같은 스레드 내에서는 인증된 사용자 정보에 접근이 가능하다.

FilterChainProxy는 요청이 처리된 이후에 다음 요청 처리에서 남아있지 않도록 SecurityContextHolder를 지워준다.

SecurityContextHolder는 SecurityContext 객체를 가지고 있다.

SecurityContext

SecurityContext는 Authentication 객체를 가지고 있다.

Authentication

Authentication은 두가지 목적을 가지고 있다.

  1. AuthenticationManager에게 인증 요청시 자격 증명을 담는 객체
  2. 인증 이후 현재 유저 정보 확인

Authentication의 구성

  • principal
  • credentials
  • authorities

principal은 사용자를 식별하기 위해 사용
username/password 기반 인증에서는 주로 UserDetails의 인스턴스이다.

credentials은 주로 비밀번호
보안을 위해 인증 이후 삭제됨

authorities는 유저에게 부여되는 상위 수준의 권한으로 roles, scopes 등이 있음

GrantedAuthority

GrantedAuthority는 사용자가 가진 고수준의 권한 또는 권한 그룹을 나타내는 객체이다.
애플리케이션 전반에서 사용자의 접근 권한을 제어하는 데 사용된다.
(도메인 객체에 대한 접근 제어는 Spring Security가 별도의 도구와 기능을 제공함)

Authentication.getAuthorities() 으로 GrantedAuthority 객체들의 컬렉션을 가져올 수 있다.
username/password 기반 인증에서는 주로 UserDetailsService로 부터 가져온다.

AuthenticationManager

AuthenticationManager는 Spring Security의 필터가 어떻게 인증을 수행하는지 정의하는 API이다.

  1. Spring Security의 필터가 자격 증명을 담아 AuthenticationManager를 호출
  2. Authentication가 리턴되고 Spring Security의 필터는 SecurityContextHolder에 Authentication을 set

ProviderManager

ProviderManager는 AuthenticationManager의 가장 흔한 구현체이다.
AuthenticationProvider 리스트에 인증을 위임한다.

각각의 AuthenticationProvider는 특정 타입의 Authentication의 인증을 처리한다.
Authentication의 타입을 처리하는 AuthenticationProvider가 없는 경우에는 ProviderNotFoundException으로 인증이 실패한다.
(ProviderNotFoundException는 AuthenticationException에 속한다.)

ProviderManager의 부모

ProviderManager는 부모 AuthenticationManager를 설정할 수 있다.
인증 처리가 불가능한 경우 AuthenticationManager에게 인증을 위임할 수 있다.

ProviderManager의 credentials 삭제

ProviderManager는 유출 방지를 위해 인증이 성공한 뒤에 Authentication에서 credentials을 삭제한다.

Authentication에서 캐시된 UserDetails 인스턴스와 같은 객체를 참조하고, credentials이 삭제된 경우 캐시된 값으로 재인증이 불가능해지는 문제가 발생한다.

eraseCredentialsAfterAuthentication를 비활성화 하면 credentials을 삭제하지 않도록 할 수 있다.

AuthenticationProvider

AuthenticationProvider는 특정 타입의 Authentication에 대한 인증 처리를 수행한다.
AuthenticationProvider는 ProviderManager에 여러개 삽입 가능하다.

DaoAuthenticationProvider는 username/password 기반 인증
JwtAuthenticationProvider는 Jwt 기반 인증

AuthenticationEntryPoint

AuthenticationEntryPoint는 클라이언트에게 credentials을 요청하는HTTP response를 보낼 때 사용한다.

AuthenticationEntryPoint의 구현체는 주로 로그인 페이지로 리다이렉트, WWW-Authenticate header를 포함한 response 등을 수행한다.

AbstractAuthenticationProcessingFilter

AbstractAuthenticationProcessingFilter는 유저의 credentials를 인증하는 기본 필터이다.

  1. 유저가 credentials을 제출하면 HttpServletRequest으로 Authentication 인스턴스를 생성한다.
  2. Authentication을 AuthenticationManager에 넘겨 인증을 진행한다.
  3. 인증이 실패하는 경우 SecurityContextHolder를 비우고 RememberMeServices.loginFail와 AuthenticationFailureHandler를 호출한다.
  4. 인증이 성공하는 경우 SessionAuthenticationStrategy에 새로운 로그인을 알리고, SecurityContextHolder에 Authentication을 set하고, RememberMeServices.loginSuccess를 호출한다. ApplicationEventPublisher가 InteractiveAuthenticationSuccessEvent을 publishes한다
    AuthenticationSuccessHandler를 호출한다.
profile
inho ha / ian(swatchon) / iha(42seoul)

0개의 댓글