自定义UserDetailsS​​ervice未被调用 - Grails& Spring Security Core [英] Custom UserDetailsService Not Being Called - Grails & Spring Security Core

查看:229
本文介绍了自定义UserDetailsS​​ervice未被调用 - Grails& Spring Security Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Spring-Security-Rest,spring-security-core和spring-security-ui插件的Grails v2.4.2。

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

  UserDetails loadUserByUsername(String username,boolean loadRoles)throws UsernameNotFoundException 

code>

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

  class CaseInsensitiveUserDetailsS​​ervice extends GormUserDetailsS​​ervice {
$ b $ / **
*使用户名不区分大小写
* /
UserDetails loadUserByUsername(String username,boolean loadRoles)抛出UsernameNotFoundException {
Person.withTransaction {status - >

log.debug不区分大小写的用户详细信息服务

//查找用户名
def user = Person.findByUsernameIlike(用户名)

//如果找不到用户,抛出异常
if(!user){
log.warn未找到用户:$ username
抛出新的UsernameNotFoundException('未找到用户',用户名)
}

集合<授权授权> authority = loadAuthorities(user,username,loadRoles)
createUserDetails user,authorities
}
}
}

我的 resources.groovy 包含:

  beans = {

userDetailsS​​ervice(com.example.core.CaseInsensitiveUserDetailsS​​ervice)

credentialsExtractor(Grails24CredentialExtractor)

//定义了一些自定义过滤器(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/userDetailsS​​ervice.html


https://github.com/grails-coder/grails-spring-security



它认为:




  • 身份验证提供程序

  • 自定义UserDetailsS​​ervice



请注意,它是在grails之上3.2.5 p>

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

解决方案

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

这篇关于自定义UserDetailsS​​ervice未被调用 - Grails&amp; Spring Security Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆