当前位置:  开发笔记 > 编程语言 > 正文

设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用

如何解决《设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用》经验,为你挑选了1个好方法。

我正在使用Spring Security和solr核心进行自己的自定义安全性,看起来我做错了但是我不确定是什么.

堆栈跟踪:

Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [5]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'authenticationManager' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'CustomUserDetails' is defined
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:231)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)

这个security-context.xml文件:




    
    
        

        

        

        

        

        
        
            
        
    



       
        
    


班级CustomUserDetails:

@Service
public class CustomUserDetails implements UserDetailsService {

    private static final Logger logger = Logger.getLogger(StatusAppJob.class);

    @Autowired
    private UserAppRepository userAppRepository;

    @Override
    public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

        List result =  getUserDetails(username);

        if(!result.isEmpty())
        {

        }
        else{
            throw new UsernameNotFoundException(username + " not found");
        }

        UserDetails user = new User(username, result.get(0).getPassword(), true, true, true, true, getAuthorities(result.get(0).getRol()));
             return user;

    }

    public Collection getAuthorities(String rol) {
        List auths = new java.util.ArrayList();
        auths.add(new SimpleGrantedAuthority(rol));
        return auths;
    }

    public List getUserDetails(String user) {
        // TODO Auto-generated method stub

        return userAppRepository.findUser(user);
    }

}

app-context.xml文件中:

    
        secturv2

        
            contextConfigLocation
            
                /WEB-INF/solr-config.xml,
                /WEB-INF/Spring-Quartz.xml,
                /WEB-INF/spring/security-context.xml,
                /WEB-INF/spring/postgres-config.xml  

            
        
    
        
            org.springframework.web.context.ContextLoaderListener
        
        
            org.springframework.web.context.request.RequestContextListener
        

        
            dispatcher
            org.springframework.web.servlet.DispatcherServlet
            
                contextConfigLocation
                /WEB-INF/spring/appServlet/servlet-context.xml
            
            1
        

        
            dispatcher
            /
        

        
            jsp
            *.html
        

        
        
            springSecurityFilterChain
            org.springframework.web.filter.DelegatingFilterProxy
        

        
            springSecurityFilterChain
            /*
        

    
        
            OpenSessionInViewFilter
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
            
                sessionFactoryBeanName
                sfTurismo
            
        
        
            OpenSessionInViewFilter
            /*
        

        
            401
            /error
        

        
            403
            /error
        

        
            500
            /error
        

    

编辑:

添加servlet-context.xml文件:




    
    
    
    
    

    

    
        
    

    
        
            
                
            
        
    

    
        
        
    

    
        
            
            
            
            
        
    


我为什么会这样

设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用

信息?



1> Dhiraj..:

这些问题是由于使用错误的名称引用bean.

请覆盖@Service注释创建的bean的名称,如下所示

@Service("CustomUserDetails")
public class CustomUserDetails implements UserDetailsService 

默认情况下,Spring会将组件的第一个字符小写 - 从"CustomUserDetails"变为"customUserDetails".您可以使用名称"customUserDetails"检索此组件.

因此,如果您不想覆盖默认名称,也可以修改以下代码

 
        
    

请参阅此链接以更好地理解此概念

可能导致问题的其他因素是您已定义了在servelet-xml中驱动的注释,该注释位于Web应用程序上下文中,其中您的服务bean已初始化.但是您已在主应用程序上下文中定义了安全性上下文.因此,只需创建一个文件给它任何名称并转移到该文件.


寻找更新的答案.你在不知不觉中陷入了这种境地.尝试了解主应用程序上下文和servlet上下文的范围.
推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有