6

I have my websocket client developed in STOMP Java client (Spring project) and server implemented in Spring boot.

When the client/server handshake happens, I am getting a connection upgrade issue.

Java Client Code

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));      

SockJsClient sockjsClient = new SockJsClient(transports);   

WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);

stompClient.setMessageConverter(new MappingJackson2MessageConverter());

stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

StompSessionHandler sessionHandler = new SessionHandler();
stompClient.connect("ws://localhost:9090/health", sessionHandler);

Server side

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {       
    stompEndpointRegistry.addEndpoint("/health")
            .setAllowedOrigins("*")
            .withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic");
    registry.setApplicationDestinationPrefixes("/app");
}

Exception generated at client side while connection to server

16:18:50.955/3771 [SimpleAsyncTaskExecutor-1] ERROR o.s.w.s.s.c.DefaultTransportRequest - No more fallback transports after TransportRequest[url=ws://localhost:9090/health/29/344d627baac949f5bab5506f05f1a7eb/websocket]
javax.websocket.DeploymentException: The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:434)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:392)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:194)
               at org.springframework.web.socket.client.standard.StandardWebSocketClient.lambda$doHandshakeInternal$0(StandardWebSocketClient.java:150)
               at java.util.concurrent.FutureTask.run(FutureTask.java:266)
               at java.lang.Thread.run(Thread.java:745)

In the tomcat localhost_access_log I see the below request info

127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/info HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 16
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/191/6828a1fdefee40cf8dc74e825d8d2b0c/websocket HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 0

I didn't find any info on how to fix this issue and what is causing it.

Please help me resolve this issue.

Mohan
  • 61
  • 1
  • 1
  • 4
  • 1
    It is pretty obvious what is happening. Each HTTP request you send is being redirected to a `login.jsp` page. The 200 is coming from when that login page is requested. So clearly you don't have the right credentials to access the WebSocket resource, or you are not sending them correctly. – Remy Lebeau Nov 01 '18 at 18:22
  • Hi thanks for the reply. i am new to websockets. how do we add credentials to the websocket client request. – Mohan Nov 02 '18 at 08:36

2 Answers2

9

Too late to be able to present a helpful resolution for your problem. Today I'm facing the same issue that you have already experienced. Unfortunately, Spring Framework samples and official resources are not referencing entirely. Therefore, I would like to write about how I made it up and running. Firstly, the original answer doesn't belong to me. I found it at https://code5.cn/so/java/783097.

As far as I can understand, to be able to upgrade the WebSocket protocol from HTTP, we should extend the "WebSocketConfiguration" part a little bit with desired capabilities.

RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
        .withSockJS();

registry.addEndpoint("/hello")
        .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
        .setAllowedOrigins("*");

After making the above adjustments, you could comprehend regarding switching between protocols through sniffing WireShark.

enter image description here

I hope it would be helpful for someone who is looking for a way to get rid of errors.

nurisezgin
  • 1,530
  • 12
  • 19
0

I fixed my project adding the websocket endpoint, from websocket config, into requestMatchers(from SecurityFilterChain) like this:

.requestMatchers("/h2-console/**", "/", "/login", "/css/**", "/js/**",
 "/img/**", "/websocket-server", "/websocket-server/**").permitAll()
Onur Kağan Aldemir
  • 585
  • 1
  • 5
  • 18