gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:51:04 25 4
gpt4 key购买 nike

我做了一个自定义的UserDetailsService,使登录名中的用户名不区分大小写,并且可以正常工作,但是,所有 Controller (以前工作过的)现在都为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)
}
}
}

最佳答案

我敢肯定这是因为创建id时没有设置GrailsUser属性。查看documentation,使用id定位authenticatedUser

关于grails - Grails Spring Security-自定义UserDetailsS​​ervice使authenticatedUser为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com