1

I am creating a java application where I need to bypass SSL authentication for some URL connection, not for all URL connection. Currently I am using the following code:

TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted (
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted (
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
} catch (Exception e) {
}

In this code all URL connection skipping SSL authentication, until the application get restarted.

Is there any way so that we can skip SSL authentication check only for some URL connection, not for all URL connection?

2
  • If you don't want it secure why use HTTPS at all? Commented May 31, 2015 at 20:42
  • there was problem in my certificate in server side, so I want to bypass SSL authentication for temporary time of period. Commented Jun 5, 2015 at 17:23

1 Answer 1

1

Instead of using HttpsURLConnection.setDefaultSSLSocketFactory globally on the class, cast your URLConnection instance into an HttpsURLConnection instance and use setSSLSocketFactory(...).

Instead of bypassing all verification, it's better to use a specific truststore for these connections too, if you can, as described in this answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.