Spring Security Java Config:使用GlobalAuthenticationConfigurerAdapter配置AuthenticationManagerBuilder失败

时间:2015-04-16 18:27:54

标签: java spring spring-security spring-java-config

我不确定如何实现GlobalAuthenticationConfigurerAdapter的子类。根据SecurityConfigurer接口的文档,init(SecurityBuilder)的实现不应该在传递的SecurityBuilder对象上设置属性。相反,这应该在configure(SecurityBuilder)方法中完成。所以我尝试了以下实现:

@Configuration
protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private WebUserDetailsService userDetailsService;

    @Autowired
    private WebUserPasswordEncoder passwordEncoder;

    @Override
    public void configure (AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(this.userDetailsService).passwordEncoder(this.passwordEncoder);
    }

}

由于框架AbstractConfiguredSecurityBuilder中的以下检查,此配置在启动期间失败:

if(buildState.isConfigured()) {
            throw new IllegalStateException("Cannot apply "+configurer+" to already built object");
        }

BuildState.isConfigured()具有以下(令人惊讶的)实现:

public boolean isConfigured() {
        return order >= CONFIGURING.order;
}

它实际上检查,如果构建当前处于CONFIGURING阶段(如javadoc中所述),但是如果它已经配置(那将是BUILT状态,我猜),如方法名称所示。

所以我的问题是:这是预期的行为还是Java配置中的错误?我在网上找到的所有其他例子通常都是在init()方法中配置构建器,所以也许我只是不能正确理解文档?

编辑:使用Spring Security 3.2.6

谢谢!

0 个答案:

没有答案