简体   繁体   English

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

[英]Grails: Spring Security Core Custom authentication getUserByUserName return null object

I'm writing a custom authorization service with Grails 2.4.4 and the Spring Security Core plugin and having trouble creating the user object below. 我正在使用Grails 2.4.4和Spring Security Core插件编写自定义授权服务,并且在创建下面的user对象时遇到了麻烦。 It's always null, but the userName and password passed into the service are valid and correct strings. 它始终为null,但是传递给服务的userName和密码是有效且正确的字符串。 (Eventually, I will replace the authentication with a custom service, but I want need to prove I can call the methods to complete that task.) (最终,我将用自定义服务替换身份验证,但我想证明自己可以调用方法来完成该任务。)

Does anybody know what I'm doing wrong? 有人知道我在做什么错吗?

The settings in Config.groovy allow me to correctly invoke this provider. Config.groovy的设置允许我正确调用此提供程序。

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

The necessary files in resources.groovy are also set correctly. resources.groovy中的必要文​​件也已正确设置。

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

Here is the service code that doesn't work... 这是无效的服务代码...

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

}

You are defining a new bean which is dependent on another bean, looks like it should be done like following code mentioned in the doc 您正在定义一个依赖于另一个bean的新bean,看起来应该像以下文档中提到的代码那样完成

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM