10

我得到了这个例外:

[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]

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

在我执行测试期间抛出了这个异常。但我无法在我的本地主机上重现它,我总是能成功构建:S

4

1 回答 1

13

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

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

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

}

我用configureGlobal方法替换了configure方法:

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

现在一切正常。

根据this answer不同的是,使用configureGlobal将允许AuthenticationManager通过全局方法安全性或另一个HttpSecurity(WebSecurityConfigurerAdapter)。

正如您在上面看到的,我同时启用了 Web Mvc 安全性和全局方法安全性。根据我的单元测试,两者都继续使用此更改,但在我的办公室这里有一些关于此更改是否正确(即,它是否正确配置全局方法安全性)或是否存在其他问题的争论解决方案。

我们认为问题的根本原因是某种类型的 Spring 错误,可能是竞争条件。尽管看起来不太可能,但问题仅在我们向项目中添加一个空类时才显现出来,而类的包或名称是什么并不重要。

于 2014-10-28T18:36:58.030 回答