Connecting to a secured websocket

后端 未结 3 1664
南旧
南旧 2020-12-28 10:44

I\'m trying to connect to a secured websocket using Jetty (or any other library).

The issue is I get a \"No trusted certificate found\" error. I\'m using a self-sign

3条回答
  •  独厮守ぢ
    2020-12-28 10:53

    Here's my Tyrus-java client using stub that I used to connect to my Jetty 9.3.6 server running HTTPS with a self-signed certificate (adapted from Tyrus websocket client, Section 8.1):

                client = ClientManager.createClient();
    
                //System.getProperties().put("javax.net.debug", "all"); // Useful for debugging SSL interaction
                // The keystore in the next two lines is the same keystore you used for running the server,
                // likely in ${jetty.base}/etc/keystore
                System.getProperties().put(SSLContextConfigurator.KEY_STORE_FILE, "/tmp/keystore");
                System.getProperties().put(SSLContextConfigurator.TRUST_STORE_FILE, "/tmp/keystore");
                // The following two passwords are what you used for your self-signed cert
                System.getProperties().put(SSLContextConfigurator.KEY_STORE_PASSWORD, "HumanReadablePassword");
                System.getProperties().put(SSLContextConfigurator.TRUST_STORE_PASSWORD, "HumanReadablePassword");
                final SSLContextConfigurator defaultConfig = new SSLContextConfigurator();
    
                defaultConfig.retrieve(System.getProperties());
                // or setup SSLContextConfigurator using its API.
    
                SSLEngineConfigurator sslEngineConfigurator = new SSLEngineConfigurator(defaultConfig, true, false, false);
                client.getProperties().put(GrizzlyEngine.SSL_ENGINE_CONFIGURATOR, sslEngineConfigurator);
                client.connectToServer(sillyWebSocketClient , ClientEndpointConfig.Builder.create().build(),
                                       new URI("wss://localhost:8443/sillyWebSocketServer/echo"));
                System.out.println ("Connected .... ");
    

    Where SillyWebSocketClient sillyWebSocketClient extends javax.websocket.Endpoint.

    I am using a Java 8/Gradle 2.7 environment, and my build.gradle looks like this:

    apply plugin: 'java'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
          compile 'javax:javaee-api:7.0'
          compile 'org.glassfish.grizzly:grizzly-core:2.3.23'
          compile 'org.glassfish.tyrus:tyrus-container-grizzly:1.2.1'
          compile 'org.glassfish.tyrus:tyrus-client:1.6'
          compile 'javax.websocket:javax.websocket-client-api:1.1'
    }
    

    Hope this helps.

提交回复
热议问题