问题描述:
服务需要调用第三方,只能生产调用
上了生产测试相关逻辑,调用第三方接口报错,错误信息:
ava.security.cert.CertificateException: No subject alternative names presen
使用的是HttpURLConnection文章来源:https://www.toymoban.com/news/detail-715855.html
问题解决
增加如下代码文章来源地址https://www.toymoban.com/news/detail-715855.html
static {
try {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
});
} catch (Exception e) {
}
}
private static void trustAllHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[1];
trustAllCerts[0] = new TrustAllManager();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
private static class TrustAllManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
到了这里,关于java.security.cert.CertificateException: No subject alternative names presen的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!