问题
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:
- http://www.stevideter.com/2012/11/17/case-insensitive-usernames-using-spring-security-core-plugin-for-grails/
- http://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html
回答1:
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
:
userDetailsService(com.example.core.CaseInsensitiveUserDetailsService) { bean->
bean.autowire = "byName"
}
回答2:
I wrote this example for educational(self) purposes, perhaps it might help: https://github.com/grails-coder/grails-spring-security
It regards to:
- Authentication Providers
- Custom UserDetailsService
Kindly notice it's on top of grails 3.2.5
来源:https://stackoverflow.com/questions/29630568/custom-userdetailsservice-not-being-called-grails-spring-security-core