如何在HandshakeInterceptor中设置Principal

时间:2015-02-17 17:14:58

标签: spring-websocket

我正在使用Spring的WebSockets非常精简的实现。 WebSocketSession有方法getPrincipal(),但是如何在HandshakeInterceptor中设置它呢?

我想将Principal放入的方法是:

public boolean beforeHandshake(final ServerHttpRequest request, final ServerHttpResponse response, final WebSocketHandler wsHandler,
        final Map<String, Object> attributes) throws Exception {
    Principal = getPrincipal();
    // Now where to set the principal so it is available in WebSocketSession?

}

1 个答案:

答案 0 :(得分:3)

registry.addEndpoint("/hello") 
.setHandshakeHandler(new DefaultHandshakeHandler() {
     @Override
     protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {

 //Here you can set and return principal that is used by websocket session.            
}