【问题标题】:Spring websocket (WSS) with angular sockjs (HTTPS) over secure web socket server (tomcat)Spring websocket (WSS) 与 angular sockjs (HTTPS) 在安全 web socket 服务器 (tomcat) 上
【发布时间】:2019-11-01 21:27:27
【问题描述】:

我正在使用服务器端的 Spring web socket + stomp + SockJsClient 和角度端的 SockJS 将消息从服​​务器端发送到角度客户端应用程序。

我的 Socket 服务器是一个 Spring Boot 应用程序,运行在 8080 端口上。

它在 ws/http 协议上运行良好。但现在我已经在套接字服务器上启用了 SSL。

Socket 服务器配置。

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/topic");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/sync").setAllowedOrigins("*").withSockJS();
}}

Java 客户端在 WS 上的工作代码

List<Transport> transports = new ArrayList<Transport>(1);
transports.add(new RestTemplateXhrTransport());
SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
String url = "ws://my-socket.server.com/sync";
StompSessionHandler sessionHandler = new MyStompSessionHandler(senderId, syncUrl, content);
stompClient.connect(url, sessionHandler);

Angular 客户端通过 HTTP 的工作代码

const Stomp = StompJs.Stomp;
const socket = new SockJS('http://my-socket.server.com/sync');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, (res, err) => {}

现在我已经通过在单独的 Spring Boot 服务器上运行的 Web 套接字服务器实现 SSL。并在服务器和客户端更新协议,如。 ws 到 wss 和 http 到 https。

还可以尝试添加 SSL 上下文

StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();
List<Transport> transports = new ArrayList<Transport>(1);
Map<String, Object> userProperties = new HashMap<String, Object>();
userProperties.put("org.apache.tomcat.websocket.SSL_CONTEXT", SSLContext.getDefault());
simpleWebSocketClient.setUserProperties(userProperties);
transports.add(new WebSocketTransport(simpleWebSocketClient));

我参考了以下堆栈链接,但没有运气:(

Secure websocket with HTTPS (SSL)

How to use Spring WebSocketClient with SSL?

请帮我摆脱它。

谢谢你:)

【问题讨论】:

  • 有什么更新吗?有同样的问题。
  • 我正在寻找相同的解决方案?有更新吗?
  • 你试过用更新的 js - sock code https one 吗?
  • @John 您在这里遇到什么问题?
  • 我也有同样的问题:/一旦我启用了 SSL,我就无法从客户端连接到 WebSocket

标签: spring-boot ssl spring-websocket stomp sockjs


【解决方案1】:

我们也遇到同样的问题,先去掉withSockJS之类的

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfigurer implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/");
        config.setApplicationDestinationPrefixes("/js");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/").setAllowedOrigins("*");
    }
}

我们的前端在 VUE 中,但在 JS 中的连接仍然相同。

    WEB_SOCKET_URL_PREFIX: 'ws://localhost:8080/'

    connect(resolve) {
        const URL = '/somePath';
        this.webSocket = new WebSocket(HttpConfig.WEB_SOCKET_URL_PREFIX);
        this.stompClient = Stomp.over(this.webSocket, { debug: false });

        this.stompClient.connect({}, () => {
            this.stompClient.subscribe(URL, data => {
                console.log(data);
            });
        });
    }

在你的java代码中你可以发送修改


    private final SimpMessagingTemplate simpMessagingTemplate;

public void someMethod(){

...
 simpMessagingTemplate.convertAndSend("/somePath", "someContent");

...
}

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,后来发现我不允许安全配置文件中的端点,只允许antMatchers中的websocket端点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2018-08-03
      • 1970-01-01
      • 2011-08-25
      • 2018-05-05
      • 2014-07-06
      相关资源
      最近更新 更多