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

John Martin
  • 173
  • 1
  • 5

2 Answers2

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

Talha Shan
  • 11
  • 3
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");

...
}
Lucas Costa
  • 99
  • 11