[Spring Security] 커스텀 필터 생성 시 'authenticationmanager must be specified'

이멀젼씨

·

2021. 5. 2. 15:00

목적


스프링 시큐리티의 커스텀 필터에서 authenticationManager를 주입하는 방법을 익히기 위함


목차


  1. authenticationManager란?
  2. 'authenticationmanager must be specified' 발생 이유
  3. 'authenticationmanager must be specified' 해결 방법

1. authenticationManager란?


authenticationManager는 authenticate라는 인증 메소드를 제공하는 인터페이스이다.


Spring Security 동작원리 참조

ProviderManagerauthenticationManager를 구현하여 Provider를 돌면서 각 Provider의 인증 메소드를 실행하여 인증을 시도한다.


즉, 인증을 시도하기 위해서 필요한 인터페이스이다.


2. 'authenticationmanager must be specified' 발생 이유


커스텀 필터를 만드는 중에 아래와 같은 에러가 발생하였다

'authenticationManager must be specified'

authenticationManager가 없기 때문에 발생하는 에러였다.


3. 'authenticationmanager must be specified' 해결 방법


현재 사용하고 있는 커스텀 필터의 부모 클래스인 AbstractAuthenticationProcessingFilter를 찾아보았다


 AbstractAuthenticationProcessingFilter document 링크


Authentication Process

The filter requires that you set the authenticationManager property. An AuthenticationManager is required to process the authentication request tokens created by implementing classes.

authenticationManager를 설정 해주어야 한다고 나온다.

필터 설정 시 setAuthenticationManager메소드 authenticationManager메소드를 실행한다.

authenticationManager메소드는 WebSecurityConfigurerAdapter에 구현되어있는 메소드로 AuthenticationManager객체를 반환해준다.


이렇게 설정을 하면 'authenticationmanager must be specified' 에러 없이 커스텀 필터가 등록이 잘 되는걸 볼 수 있다.