1

I have a need to provide an optional additional parameter to my implementation of Spring Security's UserDetailsService.

As a simple example, let's say I need to pass a Pet ID for the pet that belongs to the User.

Not all Users have pets, but if they do, my Person class that implements UserDetails has a Pet that needs to be retrieved upon login.

Here's what I tried thus far:

How to pass an additional parameter with spring security login page

The problem with the accepted answer is it feels wrong/leaky for UserDetailsService implementation to couple in awareness of HttpRequest and HttpSession.

Other approaches I've looked at involve custom AuthenticationDetails and AuthenticationDetailsSource, but this feels wrong also, since the UserDetailsService seems like it should be responsible for a single thing (loading User Details), and the Pet would be part of those User Details.

I know I can get one of these approaches to work, but they don't feel clean. Has anyone come up with a good approach on solving this problem? Not looking for anyone to write code for me, just looking for a general approach.

Thank you very much!

Community
  • 1
  • 1
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85

1 Answers1

1

I think there maybe a misunderstanding as to the purpose of UserDetails. Looking at the Javadoc UserDetails is about how the user relates to the security apparatus of your application/system. As the method summary shows; getPassword, getUsername, getAuthorities, etc, essentially who is this person (authC) and what can they do (authZ). The loading of a user's pet information would be more of a business concern and should be handled in a separate service, PetService.doesUserHavePet(User) PerService.loadPet(User), not within your implementation of UseDetailsService which should be loading an users info from your organizations directory service and/or user store. I think the reason why you are finding every approach less then clean is you are trying to incorporate two concepts/responsibilities into a single class.

Billy Korando
  • 225
  • 3
  • 6