解决出现javax.net.ssl.SSLHandshakeException: PKIX path building failed 或 sun.security.validator.Validato
2022/9/10 1:24:41
本文主要是介绍解决出现javax.net.ssl.SSLHandshakeException: PKIX path building failed 或 sun.security.validator.Validato,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
错误信息:
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
问题原因:
在Java8及高版本以上的版本在源应用程序不信任目标应用程序的证书,因为在源应用程序的JVM信任库中找不到该证书或证书链。也就是目标站点启用了HTTPS 而缺少安全证书时出现的异常
常见的解决方法有三种:
1. 手动生成证书;
2. 忽略证书验证。(建议使用)
3. 在JDK安装目录中删除
三种方式的实现:
1、手动导入安全证书
手动生成证书方法参考大佬博客:手动生成证书方法
2、忽略证书验证(建议使用)
public class Base64Util { public static String getBase64FromUrl(String fileUrl) { InputStream inputStream = null; byte[] data = null; ByteArrayOutputStream swapStream = null; HttpsURLConnection conn = null; try { URL url = new URL(fileUrl); //判断当前文件url是否是https if (fileUrl.contains("https:")){ //是https //绕过证书 SSLContext context = createIgnoreVerifySSL(); createIgnoreVerifySSL(); conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(context.getSocketFactory()); inputStream = conn.getInputStream(); }else { //当前链接是http inputStream = url.openConnection().getInputStream(); } swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inputStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(swapStream); } return new String(Base64.encodeBase64(data)); } //绕过SSL、TLS证书 public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sc = SSLContext.getInstance("TLS"); // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法 X509TrustManager trustManager = new X509TrustManager() { @Override public void checkClientTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public void checkServerTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; sc.init(null, new TrustManager[]{trustManager}, null); return sc; } }
3、删除JDK安装目录下jre/lib/security的java.security
文件的SSLv3, TLSv1, TLSv1.1
,(不建议使用,本地环境和生产环境不一样)
这篇关于解决出现javax.net.ssl.SSLHandshakeException: PKIX path building failed 或 sun.security.validator.Validato的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#