Spring Framework
[Spring Security] 인증 처리자 AuthenticationProvider
터프남
2023. 2. 23. 00:37
728x90
반응형
인증처리를 할때 가장 핵심적인 역할을 하는 클래스
AuthenticationManager가 현재 인증을 처리할 수 있는 가장 적절한 AuthenticationProvider를 찾아서 인증을 위임한다.
인증을 처리하고 자기에게 위임한 AuthenticationManager에게 인증 결과를 돌려준다.

- AuthenticationProvider는 보통 개발자들이 본인들 프로젝트 실정에 맞게 끔 구현한다.
- AuthenticationProvider는 인터페이스로 2개의 메서드를 가진다.
- authenticate 메서드는 인증 처리를 위한 검증을 한다.
- supports는 현재 인증을 처리할 수 있는 기준이 되는지를 검사한다.
- authenticate 메서드에 authentication 인자를 받는데 여기에서 사용자가 입력한 id나 password를 전달받는다.
- ID 검증은 UserDetailsService(인터페이스) 라는 서비스에서 사용자의 계정이 있는지 없는지 확인한다. 있으면 UserDetails 타입으로 반환한다. 만약 없으면 UserNotFoundException이 발생한다.
- Password 검증은 사용자가 입력한 패스워드와 저장되어있는 패스워드와 비교한다. 암호화된 패스워드를 matchs해서 비교하는데 일치하지 않으면 BadCredentialException이 발생한다.
- 그리고 나머지 추가적인 검증까지 성공하게 되면 최종적으로 Authentication에 user 정보와 권한정보인 authoritie를 담은 인증 객체를 생성한다.
- 생성한 인증객체를 자기에게 인증을 위임한 AuthenticationManager에게 돌려준다.
- 그럼 AuthenticationProvider의 역할은 끝난다.
AuthenticationProvider
public interface AuthenticationProvider {
/**
* Performs authentication with the same contract as
* {@link org.springframework.security.authentication.AuthenticationManager#authenticate(Authentication)}
* .
* @param authentication the authentication request object.
* @return a fully authenticated object including credentials. May return
* <code>null</code> if the <code>AuthenticationProvider</code> is unable to support
* authentication of the passed <code>Authentication</code> object. In such a case,
* the next <code>AuthenticationProvider</code> that supports the presented
* <code>Authentication</code> class will be tried.
* @throws AuthenticationException if authentication fails.
*/
Authentication authenticate(Authentication authentication) throws AuthenticationException;
/**
* Returns <code>true</code> if this <Code>AuthenticationProvider</code> supports the
* indicated <Code>Authentication</code> object.
* <p>
* Returning <code>true</code> does not guarantee an
* <code>AuthenticationProvider</code> will be able to authenticate the presented
* instance of the <code>Authentication</code> class. It simply indicates it can
* support closer evaluation of it. An <code>AuthenticationProvider</code> can still
* return <code>null</code> from the {@link #authenticate(Authentication)} method to
* indicate another <code>AuthenticationProvider</code> should be tried.
* </p>
* <p>
* Selection of an <code>AuthenticationProvider</code> capable of performing
* authentication is conducted at runtime the <code>ProviderManager</code>.
* </p>
* @param authentication
* @return <code>true</code> if the implementation can more closely evaluate the
* <code>Authentication</code> class presented
*/
boolean supports(Class<?> authentication);
}
참고
스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security - 인프런 | 강의
초급에서 중.고급에 이르기까지 스프링 시큐리티의 기본 개념부터 API 사용법과 내부 아키텍처를 학습하게 되고 이를 바탕으로 실전 프로젝트를 완성해 나감으로써 스프링 시큐리티의 인증과
www.inflearn.com
728x90
반응형