Custom UserDetailsService Not Being Called - Grail

2019-09-03 13:35发布

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:

  1. http://www.stevideter.com/2012/11/17/case-insensitive-usernames-using-spring-security-core-plugin-for-grails/
  2. http://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html

2条回答
迷人小祖宗
2楼-- · 2019-09-03 14:08

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"
}
查看更多
疯言疯语
3楼-- · 2019-09-03 14:08

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

查看更多
登录 后发表回答