https请求 SSL 证书校验失败:unable to find valid certification path to requested target

报错信息

1
2
3
4
5
6
7
8
9
Exception in thread "main" org.springframework.web.client.ResourceAccessException: 
I/O error on POST request for "https://ops.mall.icbc.com.cn/icbcrouter":
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target;
nested exception is javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target

在这里插入图片描述

解决方案

借用工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

/**
* Https 工具类
*
* @author 陶攀峰
* @date 2021/1/25 下午7:34
*/
public class HttpsUtils {

/**
* 忽略HTTPS请求的SSL证书(请在发送请求之前调用)
*
* @author 陶攀峰
* @date 2021/1/25 下午7:40
*/
public static void ignoreSSL() throws Exception {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType){
}

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

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
}

使用方法

在这里插入图片描述