Custom UserDetailsService Not Being Called - Grails & Spring Security Core

徘徊边缘 提交于 2020-01-16 07:41:07

问题


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

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!