我正在使用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?
}
答案 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.
}