grails - Grails Spring Websocket插件和Spring安全性

标签 grails spring-security websocket

这里的实现(http://grails.org/plugin/spring-websocket)正在运行,但是Spring Security Plugin无法识别经过身份验证的用户。我可以想象为什么这不起作用,但是有人设法使该 Controller 中的springSecurityService识别经过身份验证的用户吗?

我的 Controller 当前看起来像这样:

@Secured('hasRole("ROLE_USER")')
class WebsocketChatController {
    SpringSecurityService springSecurityService

    def index() {}

    @MessageMapping("/hello")
    @SendTo("/topic/hello")
    protected String hello(String world) {
        return "hello from controller! ( character: " + springSecurityService.currentUser + ")"
    }
}


但是springSecurityService.currentUser为null ...

最佳答案

试试这个:

@Secured('hasRole("ROLE_USER")')
class WebsocketChatController {
    def index() {}
        @MessageMapping("/hello")
        @SendTo("/topic/hello")
        protected String hello(String world, Principal principal) {
            return "hello from controller! ( character: " + principal.name + ")"
        }
    }
}

如文档中所述,第21.4.4节注释消息处理(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html)
java.security.Principal method arguments reflecting the user logged in at the time of the WebSocket HTTP handshake.

正如您从SpringSecurity知道的那样,主体将包含currentUser

关于grails - Grails Spring Websocket插件和Spring安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639449/

相关文章:

grails - 我想提供一个链接以返回 404 上的上一页

Golang Gorilla Websocket 在 120 秒时停止接收信息

java - Websocket 最大缓冲区大小

reactjs - 没有 Websockets 的 AWS AppSync 订阅

grails - 相对 URL 到服务器路径的转换 - Grails

grails - 如何通过 createCriteria 中的 desc 进行订单?

linux - 如何从 Tomcat 网络应用运行 PhantomJS

java - Spring Security 默认登录表单无法加载 CSS 文件(ERR_CONNECTION_TIMED_OUT)

java - 认证请求到/j_spring_security_check 404错误

java - Spring Boot OAuth2 单点注销(注销)