AuthenticationConfigurer应用于已构建的对

AuthenticationConfigurer应用于已构建的对

本文介绍了无法将DaoAuthenticationConfigurer应用于已构建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此异常:

  [WARN] org.springframework.web.context.support.GenericWebApplicationContext  - 异常在上下文初始化期间遇到 - 取消刷新尝试
org.springframework.beans.factory.BeanCreationException:创建名为'accountResource'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private io.ilopezluna.japanathome.service.UserService io.ilopezluna.japanathome.web.rest.AccountResource.userService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userService'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.springframework.security.crypto.password.PasswordEncoder io.ilopezluna.japanathome.service.UserService.passwordEncoder;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'securityConfiguration'的bean时出错:注入自动连接的依赖项失败;嵌套异常是java.lang.IllegalStateException:无法将org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer@54aa5730应用于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]

您可以在



在执行测试期间抛出此异常。但是我无法在我的localhost上重现它,我总是获得成功:S

解决方案

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

  @Configuration 
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true,prePostEnabled = true)
公共类SecurityConfiguration扩展WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)抛出异常{
auth.userDetailsS​​ervice(authenticationService);
}

}

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

  @Override 
public void configure(AuthenticationManagerBuilder auth)抛出异常{
auth.userDetailsS​​ervice(authenticationService);
}

现在一切正常。



根据此答案不同之处在于使用 configureGlobal 将允许全局方法安全性或其他HttpSecurity(WebSecurityConfigurerAdapter)的 AuthenticationManager 。 / p>

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



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


I'm getting this exception:

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

You can see more on https://travis-ci.org/ilopezluna/japan-at-home/builds/37866955

This exception is thrown during my execution of tests. But I can't reproduce it on my localhost, I always get a build success :S

解决方案

It took me two days, but I believe I've finally solved this. In my SecurityConfiguration class I had the following method:

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

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

}

I replaced the configureGlobal method with a configure method:

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

And now everything works fine.

According to this answer the difference is that the using configureGlobal will allow the AuthenticationManager by global method security or another HttpSecurity (WebSecurityConfigurerAdapter).

As you can see above, I am enabling both Web Mvc Security and global method security. According to my unit tests, both continue to work with this change, but there is some debate here at my office on whether this change is correct (ie, if it is configuring global method security correctly), or if there is another problem with this solution.

We believe the root cause of the problem is some type of Spring bug, possibly a race condition. As unlikely as that seems, the problem only manifested itself when we added a empty class to the project, it didn't matter what the class's package or name was.

这篇关于无法将DaoAuthenticationConfigurer应用于已构建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:38