lv.ctco.cukesrest.internal.https.TrustAllTrustManager Maven / Gradle / Ivy
The newest version!
package lv.ctco.cukesrest.internal.https;
import javax.net.ssl.*;
import java.security.*;
import java.security.cert.*;
@SuppressWarnings("SameReturnValue")
public class TrustAllTrustManager implements TrustManager, javax.net.ssl.X509TrustManager {
/**
* Method to trust all the HTTPS certificates. To be used only in the
* development environment for convenience sake
*/
public static void trustAllHttpsCertificates() {
try {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
javax.net.ssl.SSLSessionContext serverSessionContext = sc.getServerSessionContext();
serverSessionContext.setSessionTimeout(0);
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
// Explicitly do nothing
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
// Explicitly do nothing
}
}