Grails Spring Security - 自定义UserDetailsS​​ervice使authenticatedUser为null

时间:2015-03-25 14:16:49

标签: grails spring-security

我制作了一个自定义UserDetailsService,它使登录用户名中的用户名不敏感,并且它正常工作,但是,所有控制器(之前都在工作)现在为authenticatedUser返回null

在请求authenticatedUser之前,isLoggedIn()返回true,getPrincipal()返回登录用户。

这是自定义UserDetailsService

package com.mydomain

import grails.plugin.springsecurity.SpringSecurityUtils
import org.springframework.security.core.authority.GrantedAuthorityImpl
import grails.plugin.springsecurity.userdetails.GrailsUser
import grails.plugin.springsecurity.userdetails.GrailsUserDetailsService
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException

class CaseInsensitiveUserDetailsService implements GrailsUserDetailsService {

    static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]

    UserDetails loadUserByUsername(String username, boolean hasRoles) throws UsernameNotFoundException {
        return loadUserByUsername(username)
    }

    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Person.withTransaction { status ->
            Person user = Person.findByEmailIlike(username)

            if(!user) throw new UsernameNotFoundException('User not found', username)

            def authorities = user.authorities.collect {
                new GrantedAuthorityImpl(it.authority)
            }

            new GrailsUser(user.username, user.password, user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked, authorities, user.id)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我非常确定,因为您在创建id时未设置GrailsUser属性。查看documentation id用于查找authenticatedUser