17

I'm working on sending messages from the server side to angular client application using Spring web socket + stomp + SockJsClient at server side and SockJS at the angular side.

My Socket server is a spring boot application and running on 8080 port.

Its working fine over ws/http protocol. but now I have enabled SSL on socket server.

Socket Server configuration.

@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();
}}

Working code of Java Client over 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);

Working code of angular client over 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) => {}

Now I have implement SSL over my web socket server which is running on separate spring boot server. And update Protocol at server and client side like. ws to wss and http to https.

And also try following things to add SSL Context

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));

I have take reference from following stack link but no luck :(

Secure websocket with HTTPS (SSL)

How to use Spring WebSocketClient with SSL?

Please help me to get out of it.

Thank you :)

6
  • 3
    Any updates? Have the same problem. Commented Sep 24, 2019 at 13:44
  • 1
    I am looking for same solution? any updates? Commented Sep 24, 2019 at 19:06
  • Have you tried with updated js - sock code https one? Commented May 30, 2020 at 17:59
  • @John what is the issue your are facing here? Commented May 30, 2020 at 20:13
  • I have the same problem :/ As soon as I've enabled SSL I can't connect to WebSocket from the client Commented Apr 28, 2021 at 14:40

2 Answers 2

1

I also got the same issue , later found I didn't allow end points in security configuration file , just allow the websocket endpoint in antMatchers

Sign up to request clarification or add additional context in comments.

Comments

0

WE pass through the same problem, first remove withSockJS like

@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("*");
    }
}

our frontend is in VUE but the connection still the same in JS like.

    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);
            });
        });
    }

and in your java code you can send the modification with


    private final SimpMessagingTemplate simpMessagingTemplate;

public void someMethod(){

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

...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.