如何在spring-boot应用程序中正确配置更多身份验证提供程序

时间:2017-01-03 10:19:20

标签: java spring spring-boot

我有这个安全设置的应用程序:

await context.Forward(new ChildDialog(infoResult), ActivityReceivedAsync, activity, CancellationToken.None);

问题是我的daoAutentication执行了两次,我想修复。在日志中我可以看到:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthService authService;

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider());
        auth.authenticationProvider(daoAuthenticationProvider());
    }

    @Bean
    public CustomAuthenticationProvider customAuthenticationProvider() {
        return new CustomAuthenticationProvider();
    }

    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(authService);
        provider.setPasswordEncoder(new BCryptPasswordEncoder());
        return provider;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(new BasicRequestMatcher()).antMatcher("/**").authorizeRequests().anyRequest()
                .fullyAuthenticated().and().httpBasic().and().csrf().disable();
        http.addFilterBefore(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Autowired
    private AuthenticationManager authenticationManager;

    private Logger log = LoggerFactory.getLogger(ApplicationSecurity.class);

    public CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
        List<AuthenticationProvider> a = ((ProviderManager) authenticationManager).getProviders();
        log.debug("providers: " + a);
        return new CustomAuthenticationFilter(authenticationManager);
    }

}

我不知道为什么有2个DaoAuthenticationProvider。当我像这样编辑我的配置时:

2017-01-03 10:29:18.106 DEBUG 2154 --- [[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'] .r.o.MyApplication$ApplicationSecurity : providers: [org.springframework.security.authentication.dao.DaoAuthenticationProvider@4c46fcec, cz.isvs.reg.rob.ocis.auth.CustomAuthenticationProvider@24448744, org.springframework.security.authentication.dao.DaoAuthenticationProvider@60516c4c]

然后它运作正常。只有一个DaoAuthenticationProvider。问题是我不知道为什么现在这样有效,所以在我理解这种安全性如何工作之前我不想使用它

更新

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(kaasAuthenticationProvider());
    // auth.authenticationProvider(daoAuthenticationProvider());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。 This主题帮助了我很多

我正在自动装配authenticationManager:

feature

上面的帖子中没有委托:

@Autowired
private AuthenticationManager authenticationManager;

这导致:

   @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
   @Override
   public AuthenticationManager authenticationManagerBean() throws Exception {
       return super.authenticationManagerBean();
   }

之前执行
class InitializeUserDetailsManagerConfigurer
        extends GlobalAuthenticationConfigurerAdapter {
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        if (auth.isConfigured()) {
            return;
        }
        ...
}

为什么还有一个额外的DaoAuthenticationProvider