栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA微服务访问https资源的证书问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA微服务访问https资源的证书问题

JAVA微服务访问https资源的证书问题
  • 问题描述
  • 问题原因
  • 解决方案
  • 问题扩展
  • 参考链接

问题描述

Java微服务通过https下载第三方站点某个资源(比如一个图片),java抛出以下异常:

WalletServiceImpl     : 凭证图片下载失败,s**un.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
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**
    at sun.security.ssl.alert.createSSLException(alert.java:131)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:353)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:296)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:291)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:652)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:471)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:367)
    at sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:376)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:422)
    at sun.security.ssl.TransportContext.dispatch(TransportContext.java:183)
    at sun.security.ssl.SSLTransport.decode(SSLTransport.java:154)
    at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1279)
    at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1188)
......
问题原因

问题是由于java服务器上缺失第三方网站证书导致,需要利用keytool工具导入证书解决。

解决方案

1、在浏览器(以firefox为例)访问https资源(https://XXX/XXX-template/finance.jpg),点击安全连接→更多信息→查看证书看到服务证书信息。往下拉有个“下载证书链”的功能,下载pem证书链文件。
2、登录到JAVA服务器上,运行以下命令安装证书解决异常:
1). 找到JDK下cacerts文件(命令:find / -name cacerts), 例如线上服务器的位置:/usr/local/jdk1.8.0_271/jre/lib/security/cacerts
2). 拷贝浏览器导出的pem证书文件到服务器某目录下,假如:/home/XXX.pem
运行: keytool -importcert -trustcacerts -alias XXXXX -file /home/XXX.pem -keystore /usr/local/jdk1.8.0_271/jre/lib/security/cacerts
3). Java微服务重新启动。

问题扩展

1、在排查问题过程,使用curl访问https资源也报错:

curl: (60) Peer's Certificate issuer is not recognized.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

除了错误提示操作以外,可以把证书安装到信任证书目录下。
解决办法:拷贝以上提及的pem文件到目录/etc/pki/ca-trust/source/anchors/ , 再运行update-ca-trust命令解决问题。

参考链接

https://techcommunity.microsoft.com/t5/azure-database-support-blog/pkix-path-building-failed-unable-to-find-valid-certification/ba-p/2591304
https://stackoverflow.com/questions/6908948/java-sun-security-provider-certpath-suncertpathbuilderexception-unable-to-find

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号