PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilder

项目场景:

访问HTTPS协议的 api
报错
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilder

Certificate for <xxx.xxx.xxx.xxx> doesn’t match any of the subject alternative names: []


问题一

使用Java访问https协议的api报错

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilder.

原因分析:

需要配置SSL证书或者绕开ssl

解决方案:

使用浏览器访问一下api路径,然后点击左边框下载证书
在这里插入图片描述
将证书导出
进入jdk home path下的的lib下的secruity目录,导入证书

cd C:\Tools\Java\jre\lib\security

keytool -import -alias cn -keystore cacerts -file C:\Tools\Java\jre\lib\security\cn.crt

输入默认密码导入证书

changeit
y

如图所示:
在这里插入图片描述


问题二

导入证书后报错:
Certificate for <xxx.xxx.xxx.xxx> doesn’t match any of the subject alternative names: []

javax.net.ssl.SSLPeerUnverifiedException: Certificate for <xxx.xxx.xxx.xxx> doesn't match any of the subject alternative names: []
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:507)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:437)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384)
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
	at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
	at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
	at com.apig.sdk.demo.Main.main(Main.java:61)

解决方案:

修改添加如下代码:

            SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(
                           SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
                                 NoopHostnameVerifier.INSTANCE);
            client = HttpClients.custom().setSSLSocketFactory(scsf).build();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值