使用AuthenticationManagerBuilder的提供者订单

Wil*_*ler 5 java authentication spring spring-security

我正在使用Spring Security 4.0.1,并希望使用多个身份验证提供程序来使用基于Java的配置进行身份验证。如何指定提供者订单?

我希望使用AuthenticationManagerBuilder,因为这是WebSecurityConfigurerAdapter.configureGlobal()公开的内容,但是我看不到任何指定顺序的方法。我需要手动创建ProviderManager吗?

更新:这是根据阿伦的答案进行的问题澄清。我要使用的特定提供程序是ActiveDirectoryLdapAuthenticationProvider并且DaoAuthenticationProvider用于自定义UserService。

最终,我想针对第DaoAuthenticationProvider一个和ActiveDirectoryLdapAuthenticationProvider第二个进行身份验证。

AD提供程序涉及到的调用,AuthenticationManagerBuilder.authenticationProvider()而DAO提供程序涉及到的调用AuthenticationManagerBuilder.userService(),这DaoAuthenticationProvider在幕后创建了围绕用户的服务。查看源代码,它不会直接将提供程序放置在提供程序列表中(它会创建配置程序),因此Arun的答案对我不起作用。

我尝试创建DaoAuthenticationProvider手动并将其传递给authenticationProvider()。它并没有影响订单。

Aru*_*unM 2

没有明确的订购规定。AuthenticationProvider调用的顺序将是您提供to的顺序AuthenticationManagerBuilder.authenticationProvider()。有关 xml 配置,请参阅此处。这同样适用于 java 配置。

例如

auth.authenticationProvider(getAuthenticationProvider2());
auth.authenticationProvider(getAuthenticationProvider1());
Run Code Online (Sandbox Code Playgroud)

将导致以下调用顺序AuthenticationProvider2,AuthenticationProvider1

和

 auth.authenticationProvider(getAuthenticationProvider1());
 auth.authenticationProvider(getAuthenticationProvider2());
Run Code Online (Sandbox Code Playgroud)

将导致以下调用顺序AuthenticationProvider1,AuthenticationProvider2