2014-10-29 134 views
0

從這裏執行(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

回答

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. 

主要將包含currentUser,當你從SpringSecurity知道