ssl handshake exception in android gcm server

2019-04-15 16:16发布

问题:

Hi friends I am new to android. I am trying to create gcm server. my code is

System.setProperty("javax.net.ssl.trustStore","/usr/bin/keystore.jks");
String id = "cliend id";
String msg = "Test";
Sender sender = new Sender(Api key);
Message message = new Message.Builder()
        .addData("message", "this is the message")
        .addData("other-parameter", "some value")
        .build();
Result result;
result = sender.sendNoRetry(message, id);

I create keystore but still i have error like: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

I am not able to resolve it.

回答1:

Disable SSL. Add this in your code.

            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
        };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };


回答2:

You need to add the certificate for App2 to the keystore file of the used JVM located at %JAVA_HOME%\lib\security\cacerts.

First you can check if your certificate is already in the keystore by running the following command: keytool -list -keystore "%JAVA_HOME%/jre/lib/security/cacerts" (you don't need to provide a password)

If your certificate is missing you can get it by downloading it with your browser and add it to the keystore with the following command:

keytool -import -noprompt -trustcacerts -alias -file -keystore -storepass

Afer import you can run the first command again to check if your certificate was added.

Sun/Oracle information can be found here.

Hope this helped! Have Fun!