我正在设置Spring Security(v4.0.1)Web应用程序。我想拥有两个身份验证提供程序,一个“内存中”提供程序来管理管理员帐户,一个自定义身份验证提供程序,它引用我自己的实现。系统应该首先尝试针对“内存中”提供程序进行身份验证,然后针对第二个定制用户进行身份验证。我的代码如下所示:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
AuthenticationProvider provider) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("s3cr3t")
.authorities("ADMIN");
auth.authenticationProvider(provider);
}
但是,此代码使框架首先尝试我的自定义实现。这有点有意义,因为该AuthenticationManagerBuilder#authenticationProvider
方法在内部AuthenticationManagerBuilder#inMemoryAuthentication
配置Provider的同时将Provider添加到内部List 。我如何设法使其正常工作?
您可以InMemoryUserDetailsManagerConfigurer
手动创建自己的文件,并告诉它在AuthenticationManagerBuilder
完成配置时对其进行自我配置,这样它就可以AuthenticationProvider
在自定义配置之前进行安装:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
AuthenticationProvider provider) throws Exception {
inMemoryConfigurer()
.withUser("admin")
.password("s3cr3t")
.authorities("ADMIN")
.and()
.configure(auth);
auth.authenticationProvider(provider);
}
private InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder>
inMemoryConfigurer() {
return new InMemoryUserDetailsManagerConfigurer<>();
}
通常情况是,将InMemoryUserDetailsManagerConfigurer
创建,并将其添加到在AuthenticationManager
构建定制时应应用的配置程序列表中(即在安装自定义之后)AuthenticationProvider
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句