Grails Spring Websocket插件和弹簧安全

Grails Spring Websocket插件和弹簧安全

问题描述:

从这里执行(http://grails.org/plugin/spring-websocket)正在工作,但是Spring Security Plugin无法识别经过身份验证的用户。我可以想象为什么这不起作用,但是有没有人设法使这个控制器中的springSecurityService识别认证用户?Grails Spring Websocket插件和弹簧安全

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

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

+0

我想你会碰到这里的问题是Spring Security,以及springSecurityService使用ThreadLocal变量存储委托人/认证信息。该变量在HTTP请求结束时被清除。阅读Spring 4.x WebSockets的文档(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-authentication)我将导致了解这是在与原始请求不同的线程中运行,因此spring安全性已经清除了该信息。 – 2014-10-30 02:46:50

+0

启用Spring安全调试'debug'org.springframework.security''可能会告诉你弹簧安全性在做什么以及何时清除这些信息。 – 2014-10-30 02:48:18

试试这个:

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

主要将包含currentUser,当你从SpringSecurity知道