Trust all certificates in java Websocket client

First of all, I am aware of the possible risks of trusting all certificates, however for some testing purposes I have to implement this.

How can I get my client to trust all certificates? I am implementing withjavax.websocket

All I did was just connect to ws like

WebSocketContainer client = ContainerProvider.getWebSocketContainer();

try {
    session = client.connectToServer(ClientImpl.class, URI.create(uri));
} catch (DeploymentException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

      

+3


source to share


1 answer


I had the same problem. I didn't find any solution, but I was able to use a self-signed certificate.

I describe all the stages:

  • Download the certificate of the server you want to connect, you can do it from your browser (in Google Chrome, right click on the blocker next to the page url).
  • Create a keystore with the following command (remember the password you entered)

keytool -import -alias localhost -file certificate_path -keystore your_new_keystore

I recommend using ClientManager instead of WebSocketContainer . This allows you to override the hostname validation.



My code

System.getProperties().put("javax.net.debug", "all"); //usefull to understand problems

System.getProperties().put(SSLContextConfigurator.KEY_STORE_FILE, your_new_keystore_path);

System.getProperties().put(SSLContextConfigurator.TRUST_STORE_FILE, your_new_keystore_path);

System.getProperties().put(SSLContextConfigurator.KEY_STORE_PASSWORD, the_password_you_entered);

System.getProperties().put(SSLContextConfigurator.TRUST_STORE_PASSWORD, the_password_you_enterede);   

ClientManager client = ClientManager.createClient();

SslEngineConfigurator sslEngineConfigurator = new SslEngineConfigurator(new SslContextConfigurator());

sslEngineConfigurator.setHostVerificationEnabled(false); //skip host verification

client.getProperties().put(ClientProperties.SSL_ENGINE_CONFIGURATOR, sslEngineConfigurator);

client.connectToServer(you_class_with_ws_methods, your_ws_uri);

      

you_class_with_ws_methods can be the same you use with WebSocketContainer Helpful resources:

https://tyrus.java.net/documentation/1.10/user-guide.html#d0e1128 https://blogs.oracle.com/PavelBucek/entry/securing_websocket_applications_on_glassfish

+3


source







All Articles