Spring Security : Custom UserDetailsService and Custom UserDetails

Points To Remember

You need to change the
UserDetailsService, User Object of the spring security to achieve this.

Add Custom User Details to Spring Security Authentication Object

First of all we nee to create a new User
Object that will override the User class of the spring security in package org.springframework.security.core.userdetails.User After
this, you need to tell UserDetailsService to use this object as the principal for the authentication token.
For this you will have to override the UserDetailsService of the spring security.

You can have the custom
User object like. Custom User Class -> MyUser.groovy

So now we have a custom UserDetailsService that will load our MyUser object in spring security authentication
principal, instead of conventional GromUser object. You need to do one last thing to make things work. You need to
override the UserDetailsService and tell spring security to use your custom UserDetailsService and you can do it
like the following. Add this in resources.groovy

userDetailsService(MyUserDetailsService)

Thats it, now you have can add any number of custom attributes of the User object to spring security
authentication.