Grails Spring Security-自定义UserDetailsService使authenticatedUs



我制作了一个自定义的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)
        }
    }
}

我很确定这是因为在创建GrailsUser时没有设置id属性。查看文档,id用于定位authenticatedUser

最新更新