This commit is contained in:
daiqy88
2025-10-31 11:09:12 +08:00
parent 19afd704f7
commit ef24643d7f

View File

@@ -1,45 +0,0 @@
package com.ccag.utils;
import javax.net.ssl.*;
import java.security.cert.X509Certificate;
/**
* @Title: SelfSignedCertificateTrust
* @Package: com.ccag.config
* @Description:
* @author: zyj
* @date: 2025/4/27 15:18
*/
public class SSLUtil {
@SuppressWarnings("java:S4830") // 抑制SSL证书验证的Sonar警告
public static void disableSSLVerification()throws Exception{
try {
// 创建一个信任所有证书的 TrustManager
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) {}
}
};
// 安装信任所有证书的 TrustManager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 创建一个 HostnameVerifier忽略主机名验证
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// 安装 HostnameVerifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (Exception e) {
e.printStackTrace();
}
}
}