【问题标题】:Spring Websockets/Stomp upgrade to secure websocketsSpring Websockets/Stomp 升级到安全 websockets
【发布时间】:2017-02-16 08:26:05
【问题描述】:

我有 2 个使用 Spring websocket 进行通信的 Java 应用程序。我使用 ActiveMQ 版本 5.12.3。我在 TomEE 服务器上运行应用程序。这是我在 tomee.xml 中为 ActiveMQ 使用的配置:

<Resource id="MyAppMessageBus" type="ActiveMQResourceAdapter">
    BrokerXmlConfig =  broker:(tcp://localhost:61616,ws://0.0.0.0:61614,stomp://0.0.0.0:61613)
    ServerUrl       =  tcp://localhost:61616
</Resource>

这是我用于连接的 Java 代码:

    if (brokerUrl.startsWith("ws")) {
        WebSocketClient transport = new StandardWebSocketClient();
        stompClient = new WebSocketStompClient(transport);
    }

    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(taskScheduler);
    stompClient.setDefaultHeartbeat(heartbeat);
    stompClient.connect(brokerUrl, handler);

我可以使用以下代理 url 成功连接:ws://localhost:61614

我想做的是使用安全的 websocket 进行连接。当我将 tomee.xml 中的 url 更改为使用 wss:// 并更新代码中的 brokerurl 时,出现以下异常:

javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:434) ~[tomcat7-websocket.jar:7.0.68]
    at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:152) ~[spring-websocket-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:149) ~[spring-websocket-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) ~[na:1.7.0_80]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
Caused by: java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused
    at sun.nio.ch.PendingFuture.get(PendingFuture.java:202) ~[na:1.7.0_80]
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:376) ~[tomcat7-websocket.jar:7.0.68]
    ... 4 common frames omitted
Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method) ~[na:1.7.0_80]
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252) ~[na:1.7.0_80]
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198) ~[na:1.7.0_80]
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213) ~[na:1.7.0_80]
    at sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:293) ~[na:1.7.0_80]
    ... 1 common frames omitted

有人知道启用安全 websocket 的简单方法吗?我是否需要在我的代码中添加额外的传输或在我的服务器配置中添加一些东西?

【问题讨论】:

    标签: spring websocket activemq stomp wss


    【解决方案1】:

    您需要在代理配置中的服务器端有一个“wss://...”传输连接器。

    见:http://activemq.apache.org/websockets.html

    【讨论】:

      【解决方案2】:

      activemq 不支持通过标准协议的 websocket,只能通过 jetty(见https://github.com/apache/activemq/blob/master/activemq-http/src/main/java/org/apache/activemq/transport/ws/WSTransportServer.java

      你有几个选择:

      • 尝试在 tomee/lib 中添加 jetty jars(注意不要添加冲突的)
      • 配置一个外部代理(独立的 activemq)并让 tomee 连接它

      【讨论】:

      • 我已经拥有所需的 Jetty 作品,并且可以在我使用 ws:// 代理 url 时进行连接。但我想使用 wss:// 使用安全 websockets 进行连接
      猜你喜欢
      • 2016-07-26
      • 2014-03-17
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2017-01-06
      • 2017-08-23
      相关资源
      最近更新 更多