无法在GlobalMethodSecurityConfiguration中自动装配UserDetailsS​​ervice [英] Cannot autowire UserDetailsService in GlobalMethodSecurityConfiguration

查看:61
本文介绍了无法在GlobalMethodSecurityConfiguration中自动装配UserDetailsS​​ervice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了 java.lang.IllegalArgumentException:尝试在我的SecurityConfig Java Config类中自动绑定自定义UserDetailsS​​ervice实现时,必须设置UserDetailsS​​ervice.这是我的配置类的概述.

I'm getting the java.lang.IllegalArgumentException: A UserDetailsService must be set when trying to autowire custom UserDetailsService implementation in my SecurityConfig Java Config class. Here is the overview of my configuration classes.

根配置

@Configuration
@Import(value = { SecurityConfig.class, ServiceConfig.class })
public class RootConfig
{

}

ServiceConfig

ServiceConfig

@Configuration
@ComponentScan(value = "basepackage.service")
// this package includes the custom UserDetailsService implementation
// annotated by @Service
public class ServiceConfig
{

}

SecurityConfig

SecurityConfig

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends GlobalMethodSecurityConfiguration
{
    // this service is injected using setter injection, omitted for clarity
    private UserDetailsService userDetailsService;

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

现在,问题是:有时(但仅有时-似乎是完全随机的),在实例化 methodSecurityInterceptor 之前,自定义UserDetailsS​​ervice没有自动接线,我得到了 org.springframework.beans.factory.BeanCreationException:创建类basepackage.SecurityConfig ... 中定义的名称为'methodSecurityInterceptor'的bean时出错,并且堆栈结束于

Now, the problem is: sometimes (but only sometimes - it seems completely random) the custom UserDetailsService is not autowired before the instantiation of methodSecurityInterceptor and I get the org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class basepackage.SecurityConfig... and the stack ends at

Caused by: java.lang.IllegalArgumentException: A UserDetailsService must be set
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.doAfterPropertiesSet(DaoAuthenticationProvider.java:94)

正如我之前提到的, UserDetailsS​​ervice 实现使用 @Service 批注进行注释,并应由 ServiceConfig 以正确的顺序实例化..仅在部署应用程序时才会发生此问题.通常,一切都正确地实例化.当我使用 @Bean 批注在 SecurityConfig 中创建 UserDetailsS​​ervice bean时,一切都很好.但是我更喜欢将服务bean与 SecurityConfig 分开.我尝试尝试使用 @Order 批注,但没有成功.

As I mentioned previously, the UserDetailsService implementation is annotated with the @Service annotation and should be instantiated by the ServiceConfig in the right order. This problem happens only from time to time when deploying the app. Usually everything is instantiated correctly. When I create the UserDetailsService bean inside the SecurityConfig using @Bean annotation everything is fine. But I prefer keeping the services beans separated from the SecurityConfig. I tried experimenting with the @Order annotation, without success though.

为什么会发生这种情况?另外,为什么它随机发生而不是每次发生?为什么Spring不能以正确的顺序实例化bean?非常感谢您的帮助.

Any ideas why this happens? Also, why it happens randomly and not each time? Why Spring cannot instantiate the beans in the right order? I would appreciate your help.

推荐答案

这是一个非常奇怪的行为,它发生在我前一段时间.有时,它只是可以工作,而有时,它会与名称为'UserDetailsS​​ervice'的Bean必须设置为DaoAuthenticationProvider"或类似的消息打断.我通过在配置类的构造函数中添加@Autowired批注来解决此问题.

That's a very strange behavior, It happened to me a while ago. Sometimes It would just work, other times It would break with messages of "Bean of name 'UserDetailsService' must be set to DaoAuthenticationProvider" or something on those lines. I got It fixed by adding the @Autowired annotation on the constructor of my config Class.

@Autowired
public WebSecurityConfig(UserDetailsServiceImpl userDetailsService){
    this.userDetailsService = userDetailsService;
}

将注解应用到setter上是行不通的,无论如何都是非常奇怪的行为.

Applying the annotation on a setter didn't work, anyway very strange behavior.

这篇关于无法在GlobalMethodSecurityConfiguration中自动装配UserDetailsS​​ervice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆