While migrating our application from tomcat 8.0.38 to tomcat 9 we face issue with WebSocket connection and it fails with handshake exception.
2023-09-20T10:08:21.926322+00:00 gogoOS ota-docker[1174]: javax.websocket.DeploymentException: Handshake error.
2023-09-20T10:08:21.927488+00:00 gogoOS ota-docker[1174]: Caused by: org.glassfish.tyrus.core.HandshakeException: Response code was not 101: 500.
As a initial step we created one sample WebSocket application and tried deploying it on tomcat 9 in Debian 11 Linux server. The response we got is 403 forbidden error but same request works well with tomcat 8.0.38. This is very simple app but we are not able to debug what is really causing the issue in tomcat 9.
curl used and output:
curl -vvv -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: localhost" -H "Origin: http://localhost:8025" -H "Sec-WebSocket-Version: 13" -H 'Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==' http://localhost:8025/WebSocketServerExample/websocketendpoint
* Trying ::1:8025...
* connect to ::1 port 8025 failed: Connection refused
* Trying 127.0.0.1:8025...
* Connected to localhost (127.0.0.1) port 8025 (#0)
> GET /WebSocketServerExample/websocketendpoint HTTP/1.1
> Host: localhost
> User-Agent: curl/7.74.0
> Accept: */*
> Connection: Upgrade
> Upgrade: websocket
> Origin: http://localhost:8025
> Sec-WebSocket-Version: 13
> Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 403
< Content-Type: text/plain
< Content-Length: 0
< Date: Wed, 04 Oct 2023 11:42:46 GMT
<
* Connection #0 to host localhost left intact
Tomcat server.xml only following changes made on top of default tomcat 9 server.xml
-----------------------------------------------------------------------------------
<Connector port="8025" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxParameterCount="1000"
/>
web.xml only CORS filter and some mime type changes are added.
---------------------------------------------------------------------------------------
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>/*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT,PATCH,DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>1800</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>text/javascript</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ttc</extension>
<mime-type>font/collection</mime-type>
</mime-mapping>
<mime-mapping>
<extension>woff</extension>
<mime-type>font/woff</mime-type>
</mime-mapping>
<mime-mapping>
<extension>woff2</extension>
<mime-type>font/woff2</mime-type>
</mime-mapping>
<mime-mapping>
<extension>wasm</extension>
<mime-type>application/wasm</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ttc</extension>
<mime-type>font/collection</mime-type>
</mime-mapping>
----------------------------------------------------------------------------------------------------
We are facing this issue only in Linux server and in windows we could make WebSocket connection through Simple WebSocket client and Postman client.
Can anyone help us to identify the issue and help us to proceed further