본문 바로가기
FullStack/21. Java

ProviderManager,  AuthenticationProvider 차이

by nakanara 2023. 11. 23.
반응형

Srping Framework

ProviderManager, AuthenticationProvider 차이

ProviderManager와 AuthenticationProvider는 Spring Security에서 인증 관련된 구성에서 다른 역할을 수행하는 클래스들입니다.

JwtAuthenticationProvider Usage

AuthenticationProvider

AuthenticationProvider는 Spring Security에서 실제로 사용자의 인증을 처리하는 인터페이스입니다.
DaoAuthenticationProvider, LdapAuthenticationProvider, JwtAuthenticationProvider 등과 같이 다양한 구현이 있습니다.
각각의 AuthenticationProvider는 특정 유형의 인증을 처리하며, AuthenticationManager에 등록되어 사용됩니다.

AuthenticationProvider는 authenticate 메서드를 구현하고, 주어진 Authentication 객체를 기반으로 사용자의 인증을 수행합니다.
인증이 성공하면 Authentication 객체를 수정하거나 새로 생성하여 반환하고, 실패하면 AuthenticationException을 throw 합니다.

ProviderManager

ProviderManager는 AuthenticationManager의 구현 중 하나로, 하나 이상의 AuthenticationProvider를 관리하고 순차적으로 시도하여 사용자의 인증을 처리합니다.
여러 개의 AuthenticationProvider가 등록되어 있을 때, ProviderManager는 순차적으로 각각의 AuthenticationProvider에게 인증을 위임합니다. 최종적으로 어떤 AuthenticationProvider가 성공하면 그 결과로 인증이 완료됩니다.

ProviderManager는 AuthenticationProvider 목록을 가지고 있으며, 이 목록은 구성 파일이나 코드를 통해 설정됩니다.

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;

import java.util.List;

/**
 * AuthenticationManager 과 ProviderManager 관계 예시 코드
 **/
public class CustomAuthenticationManager implements AuthenticationManager {

    private final ProviderManager providerManager;

    public CustomAuthenticationManager(List<AuthenticationProvider> authenticationProviders) {
        this.providerManager = new ProviderManager(authenticationProviders);
    }

    @Override
    public Authentication authenticate(Authentication authentication) {
        return providerManager.authenticate(authentication);
    }
}

참고

반응형