简体   繁体   中英

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. It's always null, but the userName and password passed into the service are valid and correct strings. (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.

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

The necessary files in resources.groovy are also set correctly.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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