【问题标题】:How to authenticate Jetty websocket client for spring websocket (JSR-356)如何为 spring websocket (JSR-356) 验证 Jetty websocket 客户端
【发布时间】:2015-05-26 08:10:19
【问题描述】:

我们在服务器端有 Spring websocket [STOMP 和 JSR],还有 Jetty websocket 客户端与服务器通信。身份验证在浏览器中工作正常,因为我们在登录时对用户进行身份验证,我们在浏览器端有 sockjs STOMP,但我们也需要在 Jetty websocket 客户端进行身份验证。

如何做到这一点? 还有其他选择吗?

【问题讨论】:

  • @Bruno,我认为 jetty websocket 客户端没有提供在连接期间进行身份验证的方法
  • 如何认证用户?如果是 cookie,您可以通过使用 Jetty 的 HTTP 客户端执行登录操作,从服务器设置的 cookie 中检索 JESSSONID 之类的会话 ID,并将其设置为 WebSocket 握手请求,例如ClientUpgradeRequest.
  • 感谢@DonghwanKim,我们正在为 websocket 使用基本身份验证。正如您所说,我已将基本身份验证标头添加到 ClientUpdradeRequest。 :)
  • @Kunal 恭喜。如果您再次遇到此类问题,请使用 Wireshark、Fiddler 或其他工具比较每个 WebSocket 握手请求的流量。如果您这次这样做,您会发现 Jetty 的 WebSocket 握手请求中没有身份验证请求标头。这就是你应该解决的原因。编码愉快!

标签: spring spring-security spring-websocket jetty-9 java-websocket


【解决方案1】:

在spring auth配置中指定基本认证

<security:http auto-config="true" pattern="/websocket" use-expressions="true" entry-point-ref="ajaxAwareAuthenticationEntryPoint" disable-url-rewriting="false">
    <security:intercept-url pattern="/**" access="isAuthenticated()" />
    <security:http-basic/>
</security:http>

Jetty websocket 客户端 SSL 配置以及用户身份验证

URI serverURI = new URI("wss://domain:port/websocket");
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource("resources/domain.jks");
SslContextFactory sslContextFactory = new SslContextFactory();  
Resource keyStoreResource = Resource.newResource(url);
sslContextFactory.setKeyStoreResource(keyStoreResource);
sslContextFactory.setKeyStorePassword("Keystore Password");
sslContextFactory.setKeyManagerPassword("Keystore Password");   
WebSocketClient webSocketClient = new WebSocketClient(sslContextFactory);
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols("xsCrossfire");
String basicAuthHeader = HttpBasicAuthHeader.generateBasicAuthHeader("username", "password");
request.setHeader("Authorization", "Basic " + basicAuthHeader);
webSocketClient.start();

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 2017-07-20
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多