【问题标题】:How to set a Principal within a HandshakeInterceptor如何在 HandshakeInterceptor 中设置 Principal
【发布时间】:2015-11-05 13:49:16
【问题描述】:

我正在使用 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?

}

【问题讨论】:

  • 查看DefaultHandshakeHandler的源代码:如何处理RequestUpgradeStrategy。以及任何RequestUpgradeStrategy 的实现。

标签: spring-websocket


【解决方案1】:
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.            
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多