簡體   English   中英

Grails:Spring Security Core自定義身份驗證getUserByUserName返回空對象

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

我正在使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM