Secure Webcocket Tomcat
I am having problems with WebSocket connection over TLS on Tomcat 8.
I have installed the following connectors:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
/>
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystorefile="${user.home}/.keystore" keystorePass="password"
/>
Inside a standard Tomcat servlet named "socket", I used the @ServerEndpoint annotation on the class to create a WebSocket endpoint. So the abbreviated version looks like this:
@ServerEndpoint(value = "/echo")
public class EchoServer {
@OnOpen
public void onOpen(Session session) {
//
}
@OnMessage
public void onMessage(String message, Session session) {
//
}
@OnClose
public void onClose(Session session) {
//
}
}
On my client side, I have a html script connecting a WebSocket:
var webSocket = new WebSocket("ws://localhost:8080/socket/echo");
When I connect to http: // localhost: 8080 / socket everything works fine. I can open a socket connection and send messages back and forth.
Using https: // localhost: 8443 / socket I can connect to the servlet, but I cannot open a socket connection as it is an unsafe operation. Then I tried to change the connection string to:
var webSocket = new WebSocket("wss://localhost:8443/socket/echo");
but i cant get any connection.
When I look at the Tomcat docs ( https://tomcat.apache.org/tomcat-8.0-doc/web-socket-howto.html ) I don't understand what I need to change - I see mention of SSL_CONTEXT and the like, but I can't find any example of how this is used (I don't have a ClientEndpointConfig, since is it a browser?).
Can anyone point me to a solution? Why is a connection that works fine on the http port not working on the https port?
source to share
No one has answered this question yet
Check out similar questions: