I'm using Grails v2.4.2 with spring-security-rest, spring-security-core, and spring-security-ui plugins.
I've written a custom UserDetailsService to make the username case-insensitive. All I am doing is simply trying to override
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException
My com.example.core.CaseInsensitiveUserDetailsService class is defined as:
class CaseInsensitiveUserDetailsService extends GormUserDetailsService {
/**
* Make The Username Case Insensitive
*/
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
Person.withTransaction { status ->
log.debug "Case Insensitive User Details Service"
// Find The Username
def user = Person.findByUsernameIlike(username)
// If User Not Found, Throw Exception
if (!user) {
log.warn "User not found: $username"
throw new UsernameNotFoundException('User not found', username)
}
Collection<GrantedAuthority> authorities = loadAuthorities(user, username, loadRoles)
createUserDetails user, authorities
}
}
}
My resources.groovy contains:
beans = {
userDetailsService(com.example.core.CaseInsensitiveUserDetailsService)
credentialsExtractor(Grails24CredentialExtractor)
// Some Custom Filters Are Also Defined (securityContextRepository, securityContextPersistenceFilter, multipartResolver)
}
It compiles succesfully, but it never actually runs my custom CaseInsensitiveUserDetailsService. In the console, I see debug statements from the actual GormUserDetailsService instead of my custom one. What can be done to use my custom UserDetailsService?
** Note: I've been following these two tutorials:
I had a similar problem when doing something similar with a different plugin and Spring Security, and the wiring needed to be made explicitly to be by name. Try in
resources.groovy
:I wrote this example for educational(self) purposes, perhaps it might help: https://github.com/grails-coder/grails-spring-security
It regards to:
Kindly notice it's on top of grails 3.2.5