Reputation: 2030
First of all, I'm aware of the possible risks of trusting all certificates, however for some test purposes I have to implement this.
How can I force my client to trust all certificates? I'm implementing with javax.websocket
All I've done is simply connecting 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();
}
Upvotes: 4
Views: 4341
Reputation: 138
I had the same problem. I haven't found any solution, but I was able to use a self-signed certificate.
I describe all the steps:
keytool -import -alias localhost -file certificate_path -keystore your_new_keystore
I recommend you to use ClientManager instead of WebSocketContainer. This allows you to override the hostname verification.
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 Useful resources:
https://tyrus.java.net/documentation/1.10/user-guide.html#d0e1128 https://blogs.oracle.com/PavelBucek/entry/securing_websocket_applications_on_glassfish
Upvotes: 3