Grails:Spring Security Core 自定义身份验证 getUserByUserName 返回 null 对象

Posted

技术标签:

【中文标题】Grails:Spring Security Core 自定义身份验证 getUserByUserName 返回 null 对象【英文标题】:Grails: Spring Security Core Custom authentication getUserByUserName return null object 【发布时间】:2016-01-04 17:57:28 【问题描述】:

我正在使用 Grails 2.4.4 和 Spring Security Core 插件编写自定义授权服务,但无法在下面创建 user 对象。它始终为 null,但传递给服务的用户名和密码是有效且正确的字符串。 (最终,我将用自定义服务替换身份验证,但我想证明我可以调用这些方法来完成该任务。)

有人知道我做错了什么吗?

Config.groovy 中的设置允许我正确调用此提供程序。

grails.plugin.springsecurity.providerNames = ['CustomLDAPAuthProvider','daoAuthenticationProvider']

resources.groovy 中的必要文​​件也设置正确。

beans =    
    CustomLDAPAuthProvider(com.mathworks.spikeLDAP.CustomLDAPAuthService)

这是不起作用的服务代码...

package com.mathworks.spikeLDAP

import grails.gsp.PageRenderer;
import grails.transaction.Transactional

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.authentication.AuthenticationProvider
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.AuthenticationException
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Component
import org.springframework.security.core.userdetails.User

@Transactional
class CustomLDAPAuthService implements AuthenticationProvider 

    def springSecurityService       
    PageRenderer groovyPageRenderer    

    @Override
    public Authentication authenticate(Authentication arg0)
            throws AuthenticationException 

        if (arg0) 
            println ("arg0 is not null")
         else 
            println ("arg0 is null")
        

        def userName = (arg0 as UsernamePasswordAuthenticationToken).getPrincipal()
        println ("username="+userName)
        def password = (arg0 as UsernamePasswordAuthenticationToken).getCredentials()
        println ("password="+password)
        User user = springSecurityService.getUserByUserName(userName)

        def providedPassword = springSecurityService.encodePassword(password, user)
        def realPassword = springSecurityService.getUserPassword(user)

        if(!providedPassword.equals(realPassword))
            throw new BadCredentialsException("Bad login credentials!")
        

        def authorities = springSecurityService.getUserAuthorities(user)
        return new UsernamePasswordAuthenticationToken(user, arg0.credentials, authorities)

    

    @Override
    public boolean supports(Class<?> arg0) 

        return true;
    


【问题讨论】:

【参考方案1】:

您正在定义一个依赖于另一个 bean 的新 bean,看起来应该像文档中提到的以下代码一样完成

beans = 
    myBean(MyBeanImpl) 
        someProperty = 42
        otherProperty = "blue"
        bookService = ref("bookService")
    

http://grails.github.io/grails-doc/latest/guide/spring.html#theUnderpinningsOfGrails

【讨论】:

以上是关于Grails:Spring Security Core 自定义身份验证 getUserByUserName 返回 null 对象的主要内容,如果未能解决你的问题,请参考以下文章

grails-spring-security-rest 插件和悲观锁定

Grails + spring-security-core:用户登录后如何分配角色?

Grails Spring Security注释问题

Grails spring-security-oauth-google:如何设置

Grails Spring Security 插件网址

Grails/Spring Security:无法使用新创建的用户登录