Announcement Announcement Module
Collapse
No announcement yet.
Cannot find bean when implementing UserDetailsService Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot find bean when implementing UserDetailsService

    I am a newbie and have been banding my head against this code for the past couple of days. I really like how the framework is laid out and works, but this one piece of code has been a stumbling block for me.

    I am trying to implement a UserDetailsService, but for some reason the Authentication-Provider cannot find my service. The service is annotated as a service @Service("userDetailsService") and I have configured Spring to automatically find components using the "context:annotation-config" and "context:component-scan" tags in the servlet.xml config. I assumed that the service will be auto-magically loaded and that I would not have to make an explicit bean entry in any applicationContext.xml file, but...it doesn't seem to be working. I have tried all sorts of permeatations (and google searches), but nothing seems to work.

    What am I doing wrong? I am using Security 3.0.2 and Spring 3.0.5. Below are snips of my class and my config files. Any help or pointers would be appreciated.

    The error that I am getting is:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'userDetailsService' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:527)
    at
    .....

    applicationContext-security.xml
    Code:
       .....
        <global-method-security pre-post-annotations="enabled">
        </global-method-security>
    
        <http use-expressions="true" >
            <intercept-url pattern="/" access="permitAll"/>
             .....
        </http>
    
    	<beans:bean id="daoAuthenticationProvider"
    	 class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    	  <beans:property name="userDetailsService" ref="userDetailsService"/>
    	</beans:bean>
    	
    	<beans:bean id="authenticationManager"
    	    class="org.springframework.security.authentication.ProviderManager">
    	  <beans:property name="providers">
    	    <beans:list>
    	      <beans:ref local="daoAuthenticationProvider" />
    	    </beans:list>
    	  </beans:property>
    	</beans:bean>
    	
    	<authentication-manager>
    	  <authentication-provider user-service-ref="userDetailsService">
    	    <password-encoder hash="md5"/>
    	  </authentication-provider>
    	</authentication-manager>
        .....
    UserDetailsServiceImpl.java
    Code:
    package com.myapp.authentication;
    .....
    @Service("userDetailsService")
    public class UserDetailsServiceImpl implements UserDetailsService {
      @Autowired private MemberDAO memberDAO;
      @Autowired private Assembler assembler;
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username)
       ....
    web.xml
    Code:
        .....
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        .....
    spring-servlet.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"......
    	<context:annotation-config />
    	<context:component-scan base-package="com.myapp" />
    </beans>
        .....
    [/CODE]
    Thank,

    Joe

  • #2
    Try to define userDetailsService explicitly in applicationContext-security.xml via <beans:bean id="userDetailsService" ref="path.to.userDetailsService">

    Comment


    • #3
      Still having issues

      Thanks for the information. I tried that, but I still have issues. The UserDetailsServiceImpl is also auto-wired and those beans are not injected into the UserDetailsServiceImpl object. I also thought that Spring would find my beans, if i added the context:component-scan tag to the spring-servlet.xml file (which configures the Spring servlet).

      I could add those the other beans to the file, but they also have autowired properties, so I would like to avoid that.

      If you have any other ideas, please let me know.

      Thanks,

      Joe

      Comment


      • #4
        The root context (ContextLoaderListener) cannot see beans in the child context (servlet).

        So for spring security your custom service doesn't exists. You really have to put it in your xml (or you component-scanning in there).

        Comment


        • #5
          Makes Sense

          Thanks, that makes sense. I knew it was something simple like that.

          Comment

          Working...
          X