javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f

这篇具有很好参考价值的文章主要介绍了javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

原因:这是SSL证书请求问题。

原代码

private String postForAPP1(String token) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        TxnBodyCom txnBodyCom = new TxnBodyCom();
        param.put("txnBodyCom", txnBodyCom);
//        txnCom.setTxnBodyCom(txnBodyCom);
        TxnCommCom txnCommCom = new TxnCommCom();
        txnCommCom.setTRecInPage("1111");
        txnCommCom.setTxnIttChnlCgyCode("1111");
        txnCommCom.setTStsTraceId("=01111");
        txnCommCom.setTPageJump("1111");
        txnCommCom.setTxnIttChnlId("1111111");
        param.put("txnCommCom", txnCommCom);
        JSON.toJSONString(param);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(300 * 1000)
                .setConnectTimeout(300 * 1000).build();
        // 创建post方式请求对象
        HttpPost post = new HttpPost("https://www.baidu.com:1111");
        post.setConfig(requestConfig);
        // 请求的数据包为raw,设置报文头为Content-Type
        post.addHeader("Content-Type", "application/json;charset=utf-8");
        post.addHeader("C-Tenancy-id", "11111");
        post.addHeader("Connection", "keep-alive");
        post.addHeader("Referer", "https://www.baidu.com:1111");
        post.addHeader("C-Dynamic-Password-Foruser", token);
        post.addHeader("C-App-Id", "11111");

        // 装载参数
        StringEntity postingString = new StringEntity(JSON.toJSONString(param), "utf-8");
        post.setEntity(postingString);
        // 执行请求并拿到结果
        HttpResponse response = null;
        String content = null;
        CloseableHttpClient client = null;
        try {
//            httpClient = buildSSLCloseableHttpClient();
            response = httpClient.execute(post);
            // 判断返回状态是否正常
            int state = response.getStatusLine().getStatusCode();

            // 获取结果实体并返回结果
            org.apache.http.HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity);
            return content;
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.close();
        }
        return null;
    }

报错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

修改代码:新增一个方法,忽略主机名称验证

/**
 * buildSSLCloseableHttpClient:(设置允许所有主机名称都可以,忽略主机名称验证)
 * @author xbq
 * @return
 * @throws Exception
 */
private static CloseableHttpClient buildSSLCloseableHttpClient() throws Exception {
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
        // 信任所有
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    }).build();
    // ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

在原代码处调用新增的方法文章来源地址https://www.toymoban.com/news/detail-756482.html

private String postForAPP1(String token) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        TxnBodyCom txnBodyCom = new TxnBodyCom();
        param.put("txnBodyCom", txnBodyCom);
//        txnCom.setTxnBodyCom(txnBodyCom);
        TxnCommCom txnCommCom = new TxnCommCom();
        txnCommCom.setTRecInPage("10");
        txnCommCom.setTxnIttChnlCgyCode("111111");
        txnCommCom.setTStsTraceId("1111111");
        txnCommCom.setTPageJump("1");
        txnCommCom.setTxnIttChnlId("1111");
        param.put("txnCommCom", txnCommCom);
        JSON.toJSONString(param);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(300 * 1000)
                .setConnectTimeout(300 * 1000).build();
        // 创建post方式请求对象
        HttpPost post = new HttpPost("https://www.baudu.com");
        post.setConfig(requestConfig);
        // 请求的数据包为raw,设置报文头为Content-Type
        post.addHeader("Content-Type", "application/json;charset=utf-8");
        post.addHeader("C-Tenancy-id", "1111111");
        post.addHeader("Connection", "keep-alive");
        post.addHeader("Referer", "https://www.baidu.com");
        post.addHeader("C-Dynamic-Password-Foruser", token);
        post.addHeader("C-App-Id", "11111111");

        // 装载参数
        StringEntity postingString = new StringEntity(JSON.toJSONString(param), "utf-8");
        post.setEntity(postingString);
        // 执行请求并拿到结果
        HttpResponse response = null;
        String content = null;
        CloseableHttpClient client = null;
        try {
           //调用新方法
            httpClient = buildSSLCloseableHttpClient();
            response = httpClient.execute(post);
            // 判断返回状态是否正常
            int state = response.getStatusLine().getStatusCode();

            // 获取结果实体并返回结果
            org.apache.http.HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity);
            return content;
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.close();
        }
        return null;
    }

到了这里,关于javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 证书异常导致:javax.net.ssl.SSLHandshakeException: sun.security.validator

            当我们应用程序 访问设有https证书的服务 时,若 JRE未安装指定证书 则会提示标题的报错,此时有 两种方式 解决该问题:        按照实际场景,可从信息科、网络处等部门协调获取证书,也可以直接从安装证书的电脑上,通过浏览器下载证书,本文介绍后一种

    2024年01月17日
    浏览(30)
  • sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net

    报错信息: 问题描述: 在java代码中调用其他项目接口,发起的是https请求。报错信息说找不到有效证书路径。 问题解决: 信任所有SSL证书 1、新建一个SslUtil类 2、在HttpUtil工具类中修改代码 忽略HTTPS请求的SSL证书代码,必须在openConnection之前调用 解决方案参考文章https://de

    2024年02月08日
    浏览(28)
  • 关于解决驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“sun.security.validator.ValidatorException:

    首先 如果你是在访问一个网站,可以通过可以通过导入证书的方式解决该问题。或者跳过ssl证书验证。这些操作步骤,其他博主已经写的非常详细了。在这里不做赘述。 如果你只是单纯链接一个本地数据库,却莫名其妙的出现了 驱动程序无法通过使用安全套接字层(SSL)加密

    2024年02月04日
    浏览(31)
  • Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateNotYetValidException:

    生命就像人家的魔法书,涂涂改改又是一年📖 原因 解决办法 完整报错: 在执行sqoop脚本导数据的时候出现 Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateNotYetValidException: NotBefore: Tue Oct 11 17:24:18 CST 2022 报错,证书不合法,解决办法是jdbc连接MySQL时不使用ssl协议,

    2024年02月15日
    浏览(34)
  • 如何解决HTTPS请求报错sun.security.validator.ValidatorException: PKIX path building failed

    在Java中进行网络请求时出现\\\"sun.security.validator.ValidatorException: PKIX path building failed\\\"错误通常是由于SSL证书验证失败引起的。这种错误可能由以下几种原因导致: 1、证书链不完整或证书不受信任: Java使用TrustStore来验证SSL证书的有效性。如果服务器使用的SSL证书不在Java TrustS

    2024年04月13日
    浏览(34)
  • javax.net.ssl.SSLHandshakeException

    解决办法升级jdk版本或者修改jdk文件 1、对于服务器来说要支持域名并且不进行ssl证书校验,需要升级到jdk1.8的201版本及以上 2、修改…JavaJDKjrelibsecurity目录下java.security文件,添加下面语句到文件内容中

    2024年02月11日
    浏览(32)
  • Macos jdk ssl javax.net.ssl.SSLHandshakeException完美解决

    报了这么一个错误 javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake 网上一大把,测试不能用,谷歌了一下,发现少配置了一个环境变量。 System.setProperty(\\\"jdk.tls.useExtendedMasterSecret\\\", \\\"false\\\");//设置环境变量 /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/security/java.se

    2024年02月13日
    浏览(48)
  • javax.net.ssl.SSLHandshakeException: No appropriate protocol

    报错: 解释代码 这是一个Spring Boot应用程序的配置文件中的一行代码,用于设置数据库连接信息。具体解释如下: spring.datasource.url :这是Spring Boot中用于配置数据源URL的属性。 jdbc:mysql://ip:3306/dbuser :这是JDBC URL,用于指定数据库的类型(MySQL)、主机地址(ip)、端口号(

    2024年02月20日
    浏览(37)
  • 解决远程调用三方接口:javax.net.ssl.SSLHandshakeException报错

    最近在对接腾讯会议API接口,在鉴权完成后开始调用对方的接口,在此过程中出现调用报错:javax.net.ssl.SSLHandshakeException。 当你在进行https请求时,JDK中不存在三方服务的信任证书,导致出现错误javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败。

    2024年02月13日
    浏览(40)
  • Java调用Azure证书错误javax.net.ssl.SSLHandshakeException

    一、背景 Azure作为微软的公有云平台,提供了非常丰富的SDK和API让开发人员可以非常方便的调用的各项服务。公司业务需要,我们需要访问Azure上注册的应用程序,需要访问https地址 https://login.microsoftonline.com/​your-​​tenant-id 。 二、错误信息 简短报错信息:javax.net.ssl.SSLHa

    2024年02月06日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包