Spring Security - AuthenticationManager , AuthenticationProvider

TopOfTheHead·2025년 11월 18일

Spring Security

목록 보기
5/21

AuthenticationManager
Spring Security에서 HTTP Request로 전달되는 사용자 인증( Authentication )의 검증을 담당하는 인터페이스

AuthenticationProvider들을 등록 및 관리하는 역할을 수행
특정 인증방식 ( password , JWT 등 )에 대한 검증기능이 구현된 수많은 AuthenticationProvider사전등록
▶ 각 사용자가 다양한 사용자인증정보을 통한 로그인을 수행하더라도 해당 인증에 대한 검증을 제공하는 AuthenticationProvider자동으로 식별인증에 대한 검증을 수행

。보통 WebSecurityConfiguration를 담당하는 @Configuration Class 내에서 @Bean method를 통해 Spring Bean으로 등록

  // @Configuration Class
	@Bean
	public AuthenticationManager authenticationManagerBean(AuthenticationConfiguration authenticationConfiguration)
		throws Exception {
		return authenticationConfiguration.getAuthenticationManager();
	}
  • AuthenticationManager객체.authenticate(Authentication객체)
public interface AuthenticationManager {
    Authentication authenticate(Authentication authentication) throws AuthenticationException;
}

HTTP Request를 통해 credentials만 포함된 미완성된 Authentication 구현체를 전달 시 사용자Authentication검증
검증 성공principal, authorities가 추가된 완전한 Authentication 구현체를 반환
검증 실패AuthenticationException 발생.

Authentication 구현체credentials에 저장된 사용자인증 종류 ( jwt , password 등 )에 대한 적절한 검증기능을 지닌 AuthenticationProvider를 적절하게 식별 후 UserDetailsServiceUserDetails와 전달된 credentials를 비교하여 검증

AuthenticationProvider
Spring Security의 특정 사용자인증 ( jwt , password 등 )에 대한 검증을 수행하는 인터페이스
AuthenticationManager에 등록되어 활용

사용자AuthenticationProvider가 다루는 인증 방식 ( JWT )의 로그인 요청을 보낼 경우 AuthenticationManager에 의해 식별되어 검증을 수행

  • AuthenticationProvider에 의한 검증 순서


    1. Authentication 구현체Credential인증정보 ( = JWT ) 획득 및 AuthenticationManager에 의해 해당 인증정보에 적합한 AuthenticationProvider 식별
    。식별된 AutheticationProvider : JwtAutheticationProvider

    1. 해당 JWTClaims에 포함된 username를 토대로 UserDetailsService를 호출하여 DB에서 UserDetails 객체를 가져온 후 JWT 획득

    2. 해당 해싱JWT를 서로 비교검증을 수행
      검증이 성공할 경우, principal, authorities 정보를 추가로 포함한 완전한 Authentication 구현체를 반환

  • AuthenticationProvider 종류

    JwtAutheticationProvider :
    JWT 기반 인증을 처리하는 AuthenticationProvider.
profile
공부기록 블로그

0개의 댓글