//报错
Caused by: javax.net.ssl.SSLPeerUnverifiedException:
Certificate for <geo.datav.aliyun.com> doesn't match any of the subject alternative names
这个报错的意思是:SSL证书没有覆盖到ip或者域名。
尝试了信任所有证书、忽略证书认证两种方法,都没有成功。经过了多次查阅和尝试终于得到解决。参考文章:https://blog.csdn.net/RL_LEEE/article/details/87920903
核心处理方式:需要在与请求接口创建连接时关闭证书认证
public class TrustManager implements X509TrustManager {
// 不进行任何客户端证书验证
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
// 不进行任何服务器证书验证
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
public X509Certificate[] getAcceptedIssuers() {
// 返回空的证书数组,接受所有证书
return new X509Certificate[0];
}
}
//以下代码写在httpClient的参数配置中
//SSL上下文,用于初始化和信任证书
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("SSL");
TrustManager[] trustAllCerts = new TrustManager[]{new TrustManager()};
ctx.init(null,trustAllCerts,null);
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
} catch (Exception e) {
throw new RuntimeException(e);
}
//关闭https请求的主机认证
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
ctx,
new String[] { "TLSv1.2" },
null,
NoopHostnameVerifier.INSTANCE);
CloseableHttpClient closeableHttpClient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
注意:TLSv1.2对应的是jdk1.8,TLSv1.1对应jdk1.7,注意协议需要与自己的jdk版本匹配