标签: grails-platform-core

Grails springSecurityService.currentUser 返回 null

我有这个服务类别:

class UserService {

    def springSecurityService

    @Listener(topic = 'currentUser', namespace = 'auth')
    public User getCurrentUser(){
        return springSecurityService.currentUser
    }

    def authenticate(OAuthToken oAuthToken){
        SecurityContextHolder.context.authentication = oAuthToken
        return springSecurityService.currentUser
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用身份验证方法,springSecurityService.currentUser则返回当前用户。但如果我已经登录,我想使用getCurrentUser方法获取当前用户,它返回 null。我调试了该Springsecurityservice.getCurrentUser方法,方法“isLoggedIn”返回 false。

但如果我在我看来尝试这样做,它会起作用:

<sec:ifLoggedIn>Yeeees</sec:ifLoggedIn>
Run Code Online (Sandbox Code Playgroud)

更新:

当我在 SecurityService 中运行UserService.getCurrentUserSCH.context.authentication 时返回 null。在该调用之后,视图将被渲染并SCH.context.authentication返回正确的身份验证。

更新2:

我分析了它,似乎只有当我将它与事件总线一起使用时才会发生。如果我直接从控制器调用 getCurrentUser,它工作正常,但如果我使用事件总线,它就不行。我使用平台核心插件中的事件总线。事件总线还有另一个 SCH.context 吗?

grails spring-security grails-platform-core

2
推荐指数
1
解决办法
2444
查看次数