自定义UserDetailsS​​ervice未被调用 - Grails& Spring安全核心

时间:2015-04-14 14:51:19

标签: grails spring-security userdetailsservice

我正在使用带有spring-security-rest,spring-security-core和spring-security-ui插件的Grails v2.4.2。

我编写了一个自定义UserDetailsS​​ervice来使用户名不区分大小写。我所做的只是试图覆盖

UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException 

我的 com.example.core.CaseInsensitiveUserDetailsS​​ervice 类定义为:

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
        }
    }
}

我的 resources.groovy 包含:

beans = {

    userDetailsService(com.example.core.CaseInsensitiveUserDetailsService)

    credentialsExtractor(Grails24CredentialExtractor)

    // Some Custom Filters Are Also Defined (securityContextRepository, securityContextPersistenceFilter, multipartResolver)

}

它成功编译,但它实际上从未运行我的自定义 CaseInsensitiveUserDetailsS​​ervice 。在控制台中,我看到来自实际GormUserDetailsS​​ervice的调试语句而不是我的自定义调试语句。如何使用我的自定义UserDetailsS​​ervice?

** 注意:我一直在关注这两个教程:

  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 个答案:

答案 0 :(得分:0)

在使用不同的插件和Spring Security做类似的事情时,我遇到了类似的问题,并且需要明确地通过名称进行布线。试试resources.groovy

userDetailsService(com.example.core.CaseInsensitiveUserDetailsService) { bean->
    bean.autowire = "byName"
}

答案 1 :(得分:0)

我为教育(自我)目的写了这个例子,也许它可能有所帮助: https://github.com/grails-coder/grails-spring-security

它关注:

  • 身份验证提供商
  • 自定义UserDetailsS​​ervice

请注意它是在grails 3.2.5

之上