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

标签 grails spring-security authorization

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

有人知道我在做什么错吗?
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;
    }

}

最佳答案

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

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

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

关于grails - Grails:Spring Security Core自定义身份验证getUserByUserName返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32993302/

相关文章:

mysql - 使用 Hibernate 实现 JWT Spring 安全 token 存储?

python - Django:如何仅获取与登录用户所属区域匹配的记录?

grails - Gradle插件不适用于Grails 2.2.3

oracle - 在 grails 数据库迁移中使用 PL SQL

rest - grails 2.4.4 @Resource为所有URL提供404

java - 使用 java 配置进行 n 因素身份验证

spring - 在其他模式之前定义了通用匹配模式('/**')

Jenkins 管道手动步骤权限

authorization - 根据授权限制维护 View 输出

grails - Grails-构建配置错误