Cyb*_*eks 2 grails spring-security grails-platform-core
我有这个服务类别:
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 吗?
我找到了解决方案。问题是事件总线在线程内工作,默认情况下 Spring 上下文不会传递到线程。
配置中的选项“上下文持有者策略”解决了这个问题:
grails.plugin.springsecurity.sch.strategyName = org.springframework.security.core.context.SecurityContextHolder.MODE_INHERITABLETHREADLOCAL
Run Code Online (Sandbox Code Playgroud)
http://grails-plugins.github.io/grails-spring-security-core/latest/#miscProperties
| 归档时间: |
|
| 查看次数: |
2444 次 |
| 最近记录: |