Grails的springSecurityService.currentUser返回null [英] Grails springSecurityService.currentUser returns null

查看:153
本文介绍了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
}
}

如果我使用authenticate方法, springSecurityService.currentUser 将返回当前用户。但是,如果我已经登录,我想用 getCurrentUser 方法获取当前用户,并且它返回null。我调试了 Springsecurityservice.getCurrentUser 方法,并且方法isLoggedIn返回false。

但是,如果我尝试使用我认为它的工作原理:

 < sec:ifLoggedIn> Yeeees< / sec:ifLoggedIn> 

更新:

运行 UserService.getCurrentUser 时,SecurityService中的SCH.context.authentication返回null。在该调用之后,视图正在呈现,并且 SCH.context.authentication 返回一个正确的认证。



更新2:



我分析了它,它似乎只在我用于事件总线时才会发生。如果我直接从控制器调用getCurrentUser,它可以正常工作,但如果我使用事件总线,它不会。
我使用来自platform-core插件的Event Bus。是否有另一个事件总线的SCH.context?

解决方案

我找到了一个解决方案。问题在于事件总线在线程内工作,并且默认情况下,spring上下文不会传递给线程。



在配置文件中选择'上下文持有者策略'解决了这个问题:

  grails.plugin.springsecurity.sch.strategyName = org.springframework.security.core.context.SecurityContextHolder.MODE_INHERITABLETHREADLOCAL 

http://grails-plugins.github。 io / grails-spring-security-core / latest /#miscProperties

I have this service class:

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
    }
}

If I use the authenticate method, springSecurityService.currentUser returns the current user. But if I am already logged in I want to get a current user with getCurrentUser method and it returns null. I debugged the Springsecurityservice.getCurrentUser method and the method "isLoggedIn" returns false.

But if I try this in my view it works:

<sec:ifLoggedIn>Yeeees</sec:ifLoggedIn>

UPDATE:

When I run UserService.getCurrentUser the SCH.context.authentication in SecurityService returns null. After that call, the view is being rendered and SCH.context.authentication returns a right authentication.

UPDATE 2:

I analysed it and it seems to happen only if I use it with event bus. If I call getCurrentUser directly from controller, it works fine, but if I use event bus it doesn't. I use Event Bus from platform-core plugin. Is there another SCH.context for Event Bus?

解决方案

I found a solution. The problem was that Event Bus works within a thread and spring context will not be delivered to threads by default.

The option 'context holder strategy' in config solve this:

grails.plugin.springsecurity.sch.strategyName = org.springframework.security.core.context.SecurityContextHolder.MODE_INHERITABLETHREADLOCAL

http://grails-plugins.github.io/grails-spring-security-core/latest/#miscProperties

这篇关于Grails的springSecurityService.currentUser返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆