gpt4 book ai didi

java - 通过 SSLSocketFactory 连接时接受 Java 自签名证书

转载 作者:行者123 更新时间:2023-12-01 22:51:38 24 4
gpt4 key购买 nike

这里有一些关于如何接受 Java 自签名 SSL 连接的答案。但是当通过 SSLSocketFactory 连接到服务器时,这些都不起作用。谁能给我示例代码吗?

最佳答案

创建一个如下所示的类。此 Java 代码将使您能够接受所有证书

        public static class NukeSSLCerts {
protected static final String TAG = "NukeSSLCerts";

public static void nuke() {
try {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] myTrustedAnchors = new X509Certificate[0];
return myTrustedAnchors;
}

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

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
} catch (Exception e) {
}
}

}

然后像这样执行

    new NukeSSLCerts().nuke();

关于java - 通过 SSLSocketFactory 连接时接受 Java 自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58455696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com