All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tourniquet.restclient.SSLConfiguration Maven / Gradle / Ivy

package io.tourniquet.restclient;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 * Helper class to globally disable checks for proper certificates on HTTPS connections.
 * Caution: use only for testing purposes.
 */
public class SSLConfiguration {

    /**
     * Disables SSL Certificate checks globally for all HttpsUrlConnections.
     */
    public static void ignoreSSL() {

        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {

                    public X509Certificate[] getAcceptedIssuers() {

                        return null;
                    }

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

                    }

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

                    }
                }
        };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy