com.arextest.schedule.utils.SSLUtils Maven / Gradle / Ivy
package com.arextest.schedule.utils;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import lombok.extern.slf4j.Slf4j;
/**
* Created by Qzmo on 2023/8/7
*/
@Slf4j
@SuppressWarnings("squid:S4830")
public class SSLUtils {
// this is dangerous, but this application should only be used in a trusted environment
public static void disableSSLVerification() {
try {
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) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// trust all
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
} catch (Exception e) {
LOGGER.error("Ignore SSL cert check failed", e);
}
}
}