gpt4 book ai didi

grails - springSecurityService.isLoggedIn() 在 @MessageMapping 函数中返回 false

转载 作者:行者123 更新时间:2023-12-02 15:40:50 25 4
gpt4 key购买 nike

我正在尝试一个简单的聊天,需要用户登录才能使用。我已经使登录工作正常了,并且可以使用 springSecurityService.isLoggedIn() 在 Controller 中检测到它。但是,在聊天处理程序中调用该函数时,它将始终返回 false。有问题的功能:

@MessageMapping("/sendchat")
@SendTo("/topic/chats")
protected String sendchat(String getrequest) {
log.info "\n\npre login"
if(springSecurityService.isLoggedIn())
{
log.info "\n\n"+getrequest
//do other things if successful
}
else
{
log.info "\n\nnot logged in"
}

}

即使 Controller 的 index() 函数正确检测到用户已登录,它也会始终执行 else 语句。

最佳答案

那是因为“springSecurityService”仅适用于Controller Action 方法,“spring-security-core插件”使用过滤器设置安全上下文,然后springSecurityService可以检索用户登录状态。

在您的代码中,该方法是“ protected ”并且它是“websocket 插件”约定,因此您应该根据插件文档进行安全保护,如下所示:

class ExampleController {

@ControllerMethod
@MessageMapping("/hello")
@PreAuthorize("hasRole('ROLE_USER')")
@SendTo("/topic/hello")
String hello(String world) {
return "hello from secured controller, ${world}!"
}

@ControllerMethod
@MessageExceptionHandler
@SendToUser(value = "/queue/errors", broadcast = false)
String handleException(Exception e) {
return "caught ${e.message}"
}

}

更多细节可以引用“ https://github.com/zyro23/grails-spring-websocket#securing-message-handler-methods”。

关于grails - springSecurityService.isLoggedIn() 在 @MessageMapping 函数中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61525359/

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