본문 바로가기
Spring/Security

[Spring] Spring Security Authentication이란?

by 기몬식 2023. 12. 10.

Authentication

public interface Authentication extends Principal, Serializable {

    Collection<? extends GrantedAuthority> getAuthorities();

    Object getCredentials();

    Object getDetails();

    Object getPrincipal();

    boolean isAuthenticated();

    void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

Authentication 은 사용자의 인증 정보를 나타내는 인터페이스로 아래와 같은 내용을 관리합니다.

  • Authorities: 인증 객체의 권한을 관리합니다. 역할(role)이나 권한(authority)에 대한 명시며 AuthenticationManager에 의해 인가 되지 않은 사용자는 빈 컬렉션을 반환합니다.
  • Credentials: 인증 객체의 자격 증명을 위해 사용되는 증명서 객체로 주로 비밀번호나 암호화된 토큰 등을 포함하여 사용합니다.
  • Details: 인증 객체에 대한 세부 정보를 저장하기 위해 사용되는 객체로 IP 주소, 인증서 일련 번호 등으로 사용합니다. 사용되지 않는 경우 null을 반환합니다.
  • Principal: 인증 객체에 대한 신원을 저장하기 위해 사용되는 객체로 사용자 이름과 비밀번호가 포함된 인증 요청의 경우 이는 사용자 이름이 됩니다.

위와 같은 정보를 가지고 있는 Authentication 객체는 인증 요청 시 AuthenticationManager 에 의해 명시적으로 setAuthenticated() 메소드를 호출 받아 최종적으로 인증을 받게 됩니다. 이를 위해 AuthenticationManager 는 authenticate() 메소드를 제공하는데 AuthenticationManager 의 동작 방식은 다음과 같습니다.

AuthenticationManager

AuthenticationManager는 SpringSecurityFilter 가 인증을 수행할 수 있도록 제공되는 API로 인증 객체를 인증 시도하고 완료 되면 권한이 부여 된 인증 객체를 반환합니다. 표준 구현체로는 ProviderManager가 제공됩니다.

ProviderManager

출처

ProviderManager는 내부적으로 AuthenticationProvider를 컬렉션으로 가지고 있으며 인증과 관련 된 모든 동작을 위임합니다.

AuthenticationProvider 는 일반적으로 null이 아닌 응답을 제공할 때까지 순서대로 동작합니다. null이 아닌 응답은 AuthenticationProvider 에게 인증 요청을 결정할 권한이 있으며 더 이상 AuthenticationProvider 가 인증을 시도하지 않습니다.

AuthenticationProvider

AuthenticationManager.authenticate() 메소드를 실질적으로 실행하는 클래스로 전달 받은 인증 객체를 인증합니다. supports() 메소드를 통해 AuthenticationProvider 가 인증 객체를 인증할 수 있는지 없는지 여부를 판단할 수 있습니다.

또한 다음과 같이 사용자 정의 AuthenticationProvider 를 등록할 수 도 있습니다.

   @Bean
    AuthenticationManager authenticationManager() {
        return new ProviderManager(List.of(firstAuthenticationProvider));
    }

AuthenticationProvider 를 상속 받아 사용자 정의 FirstAuthenticationProvider 를 정의한 후 AuthenticationManager 에 등록 시킵니다.

다음은 기본으로 제공하는 주요 AuthenticationProvider 에 대한 설명입니다.

  • AnonymousAuthenticationProvider: 익명 사용자를 위한 AnonymousAuthenticationToken의 검증을 지원하며 인증 처리합니다.
  • RemeberMeAuthenticationProvider: rememberMe 토큰을 위한 RemeberMeAuthentcationToken의 검증을 지원하며 인증 처리합니다.
  • AbstractUserDetailsAuthenticationProvider: Principal에서 userName을 찾고 caching 된 값이 없다면 UserDetailsService 에서 사용자 정보를 조회한 후 UsernamePasswordAuthenticationToken 을 생성합니다.

오탈자 및 오류 내용을 댓글 또는 메일로 알려주시면, 검토 후 조치하겠습니다.

'Spring > Security' 카테고리의 다른 글

[Spring] Spring Security SecurityFilter란?  (1) 2023.12.03
[Spring] Spring Security 구성 요소  (0) 2023.09.20
[Spring] Spring Security란?  (1) 2023.09.14