1

I'm trying to connect to one of my servers through ssl, with Java. I tried a lot of options

String jwtUrl = "https://ABC.XYZ.COM:3333/api/auth";
String username = "ABC";
String password = "ABC";


String userCredentials = username + ":" + password;
String basicAuth = "Basic "
+ new String(org.apache.commons.codec.binary.Base64.encodeBase64(userCredentials.getBytes()));

HttpHeaders headers1 = new HttpHeaders();
headers1.set("Authorization", basicAuth);
HttpEntity httpEntity = new HttpEntity(headers1);
ResponseEntity<String> responseEntity = restTemplate1.exchange(jwtUrl, HttpMethod.GET, httpEntity,
String.class);

System.out.println("Response Body = " + responseEntity.getBody());

I am getting this exception ,

Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateEx
ception: No name matching ABC.XYZ.COM found
        at sun.security.ssl.Alerts.getSSLException(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
        at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
        at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
        at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
        at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
        at sun.security.ssl.Handshaker.processLoop(Unknown Source)
        at sun.security.ssl.Handshaker.process_record(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source
)

this way it works for me , for testing purpose

static {
        disableSslVerification();
}

private static void disableSslVerification() {
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    // TODO Auto-generated method stub

                }
            } };

            // 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() {

                @Override
                public boolean verify(String hostname, SSLSession session) {
                    // TODO Auto-generated method stub
                    return false;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }

How to solve this in good way. i cannot disable ssl verification, what are other options available ? I am using spring Rest Template ,tomcat etc I don't want to entirely ignores certificate checking.

Swadeshi
  • 1,596
  • 21
  • 33
  • Make sure the certificate is in your trust store file. Mention it to your java program. Refer http://javarevisited.blogspot.com/2012/03/add-list-certficates-java-keystore.html for more details – Balaji Katika Mar 22 '16 at 09:38
  • Try to check this answer https://stackoverflow.com/questions/4072585/disabling-ssl-certificate-validation-in-spring-resttemplate/61686589#61686589 – Oleg Maksymuk May 09 '20 at 11:43

1 Answers1

0

Make sure the certificate is in your trust store file. Your JRE's trustStore is by default in JAVA_HOME/jre/lib/security folder and commonly named as "cacerts". Import your server certificate to this file and specify it through javax.net.ssl.trustStore JVM property. Refer javarevisited.blogspot.com/2012/03/… for more details

Balaji Katika
  • 2,835
  • 2
  • 21
  • 19
  • But in my case ,server is not providing me certificate – Swadeshi Mar 28 '16 at 06:03
  • Try these links http://serverfault.com/questions/139728/how-to-download-the-ssl-certificate-from-a-website http://superuser.com/questions/97201/how-to-save-a-remote-server-ssl-certificate-locally-as-a-file – Balaji Katika Mar 28 '16 at 07:53