无法将DaoAuthenticationConfigurer应用于已构建的对象

ilo*_*una 9 java spring spring-security jhipster codeship

我得到了这个例外:

[WARN] org.springframework.web.context.support.GenericWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountResource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private io.ilopezluna.japanathome.service.UserService io.ilopezluna.japanathome.web.rest.AccountResource.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.crypto.password.PasswordEncoder io.ilopezluna.japanathome.service.UserService.passwordEncoder; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer@54aa5730 to already built object
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
Run Code Online (Sandbox Code Playgroud)

您可以在https://travis-ci.org/ilopezluna/japan-at-home/builds/37866955上查看更多信息

在执行测试期间抛出此异常.但我不能在我的本地主机上重现它,我总是获得成功:S

JBC*_*BCP 12

我花了两天时间,但我相信我终于解决了这个问题.在我的SecurityConfiguration班上,我有以下方法:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(jsr250Enabled=true, prePostEnabled=true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(authenticationService);
    }

}
Run Code Online (Sandbox Code Playgroud)

configureGlobal用一种configure方法替换了方法:

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(authenticationService);
    }
Run Code Online (Sandbox Code Playgroud)

现在一切正常.

根据这个答案的不同之处在于,using configureGlobal将允许AuthenticationManager通过全局方法安全性或另一个HttpSecurity(WebSecurityConfigurerAdapter).

如您所见,我正在启用Web Mvc Security和全局方法安全性.根据我的单元测试,两者都继续使用这个更改,但在我的办公室有一些争论,这个更改是否正确(即,是否正确配置全局方法安全性),或者是否存在其他问题解.

我们认为问题的根本原因是某种类型的Spring bug,可能是竞争条件.尽管看起来不太可能,但只有当我们在项目中添加一个空类时,问题才会出现,这个类的包或名称是什么并不重要.

  • 您可能遇到了循环bean引用,当BeanPostProcessors使用(方法安全性)时,它们处理得不是很好.如果您有一个重现问题的示例,我建议您在https://jira.spring.io/browse/SEC/上报告错误.您使用的是Boot,如果是,则可能与https://相关jira.spring.io/browse/SEC-2661 (3认同)