JAVA/Spring
spring boot 2.0 에서 security 사용시 AuthenticationManager @Autowired 안될때
cocho/kuby
2018. 8. 15. 21:22
spring boot 1.5 버전에서 잘 되던
AuthenticationManager 를 못가져 올 수 있다.
spring security 의 인증 로직을 담당하는 부분인데
이부분을 노출시켜서 다른 매커니즘에 주입을 시켜야 하는데
@Autowired 를 하면 AuthenticationManager 를 찾을 수 없다고 나온다.
Spring boot 2.0 마이그레이션 가이드 를 보면
AuthenticationManager Bean
If you want to expose Spring Security’s AuthenticationManager
as a bean, override the authenticationManagerBean
method on your WebSecurityConfigurerAdapter
and annotate it with @Bean
.
즉, AuthenticationManager 를 노출시키고 싶으면
WebSecurituConfigurerAdapter 를 상속하는 Security Config 클래스를 만들고
WebSecurityConfigurerAdapter 의 authenticationManagerBean 를 오버라이드 하고
bean 으로 등록 후에 사용 해야 한다고 한다.
이하 예시
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}