【发布时间】:2018-08-30 20:19:51
【问题描述】:
我正在使用 Spring Boot 2.0(利用 Spring Security 5.0)。我正在尝试将自定义 AuthenticationProvider 添加到我的 WebSecurityConfigurerAdapter 子类中的 AuthenticationManager 。如果我重写 configure(AuthenticationManagerBuilder) 方法来提供我的新提供程序,那么我不知道如何将 AuthenticationManager 作为 bean 检索。
例如:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.authenticationProvider(customAuthenticationProvider);
}
}
customAuthenticationProvider 在哪里实现AuthenticationProvider。
在Spring docs中,好像指明两者不兼容:
5.8.4 AuthenticationProvider 您可以通过将自定义 AuthenticationProvider 公开为 bean 来定义自定义身份验证。例如, 以下将自定义身份验证假设 SpringAuthenticationProvider 实现 AuthenticationProvider:
[注意] 仅当 AuthenticationManagerBuilder 没有 已填充
确实,如果我尝试使用以下方法检索 AuthenticationManager bean:
@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}
那么configure() 方法甚至都不会被调用。
那么如何将我自己的自定义提供程序添加到默认提供程序列表中,并且仍然能够检索 AuthenticationManager?
【问题讨论】: