Results 1 to 5 of 5

Thread: Cannot find bean when implementing UserDetailsService

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Question 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. #2
    Join Date
    Apr 2010
    Posts
    11

    Default

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

  3. #3
    Join Date
    Dec 2010
    Posts
    3

    Default 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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Dec 2010
    Posts
    3

    Default Makes Sense

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •