Grails Spring Websocket插件和弹簧安全

时间:2014-10-29 19:56:01

标签: grails spring-security websocket

此处的实现(http://grails.org/plugin/spring-websocket)正在运行,但Spring Security Plugin无法识别经过身份验证的用户。我可以想象为什么这不起作用,但有没有人设法让这个控制器中的springSecurityService识别认证用户?

我的控制器目前看起来像这样:

@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 ...

1 个答案:

答案 0 :(得分:1)

试试这个:

@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.

Principal将包含currentUser,正如您在SpringSecurity中所知道的那样