Announcement Announcement Module
No announcement yet.
injecting custom userDetailsService into component Page Title Module
Move Remove Collapse
This topic is closed
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • injecting custom userDetailsService into component

    I am a bit confused about how to do this so I will just explain what I am trying to do and hopefully someone can point me in the right direction.

    We use a custom userDetailsService in our Spring Security implementation. I want to inject that bean into another service bean I have so that I can manually call the 'loadUserByUsername(String username)' method to return a UserDetails interface and then authenticate the user like this:

    Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
            SecurityContext securityContext = SecurityContextHolder.getContext();
    When I try this however, the application context fails to load because it cannot autowire the userDetailsService bean onto my service bean. I have not figured out the problem yet but in tests I have seen clues that it might have to do with the proxy of the userDetailsService bean implementing additional aop interfaces...

    Can this be done at all or is this the wrong approach?

    I need to call the loadByUsername method from one of my service beans because that method has custom logic in it.

  • #2
    Sounds like you are just trying to inject the UserDetailsService into another bean, which should work without any issue and is no different from using any other Spring beans.

    It's impossible to say what the problem might be without more information on the error, configuration etc.


    • #3
      Thanks for your help. The problem was just that I was trying to autowire my custom bean as an instance of its class, but I needed to autowire it as an instance of its interface: lsService