报错 unable to find valid certification path to requested target executing

这篇具有很好参考价值的文章主要介绍了报错 unable to find valid certification path to requested target executing。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

提示信息:

审核失败!sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target executing POST 。。。 。。。

出现原因
这个问题的根本原因是你安装JDK时,Java\jar 1.8.0_141\lib\ext\里面缺少了一个安全凭证jssecacerts证书文件,通过运行下面类可以生成证书,将生成的证书放在Java\jar 1.8.0_141\lib\ext\这个目录下,重启编译器就可以解决。

解决方式有两种 第一种直接在发送请求的时候忽略掉http证书验证,第二种方式本地下载个证书

第一种:解决方式是在发送http请求的时候,可以过滤掉所有的https证书验证。

/**
 * POST请求
 *
 * @param url
 * @param data
 * @return
 */
public String sendPost() {
    LOG.info("send to tanggong : " + data);
    String result = null;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    //isNoSSL是否需要走特殊方法
    if(isNoSSL) 
    { 
        httpClient = (CloseableHttpClient)wrapClient(httpClient); 
    }
    HttpPost post = new HttpPost(httpUrl);
    post.addHeader("Content-type","application/json; charset=utf-8");
    post.setHeader("Accept", "application/json");
    CloseableHttpResponse response = null;
    try {
        StringEntity sen = new StringEntity(data.toString(),Charset.forName("UTF-8"));
        sen.setContentEncoding("UTF-8");
        sen.setContentType("application/json");
        post.setEntity(sen);
        response = httpClient.execute(post);
        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        }
        LOG.info("ZP return :" + result);
        return result;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            httpClient.close();
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
 
/**
 * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”异常
 * 不用导入SSL证书
 * @param base
 * @return
 */ 
public static HttpClient wrapClient(HttpClient base) { 
    try { 
        SSLContext ctx = SSLContext.getInstance("TLS"); 
        X509TrustManager tm = new X509TrustManager() { 
        
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {
                // TODO Auto-generated method stub
                 
            }
 
            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {
                // TODO Auto-generated method stub
                 
            }
 
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                // TODO Auto-generated method stub
                return null;
            } 
        }; 
        ctx.init(null, new TrustManager[] { tm }, null); 
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx,NoopHostnameVerifier.INSTANCE); 
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(ssf).build(); 
        return httpclient; 
    } catch (Exception ex) { 
        ex.printStackTrace(); 
        return HttpClients.createDefault(); 
    } 
} 

第二种方式:

1、生成安全证书
2、放入jre相应路径下

生成安全证书

import java.io.*;
import java.net.URL;
 
import java.security.*;
import java.security.cert.*;
 
import javax.net.ssl.*;
 
public class InstallCert {
 
    public static void main(String[] args) throws Exception {
        String host;
        int port;
        char[] passphrase;
        if ((args.length == 1) || (args.length == 2)) {
            String[] c = args[0].split(":");
            host = c[0];
            port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
            String p = (args.length == 1) ? "changeit" : args[1];
            passphrase = p.toCharArray();
        } else {
            System.out.println("Usage: java InstallCert <host>[:port] [passphrase]");
            return;
        }
 
        File file = new File("jssecacerts");
        if (file.isFile() == false) {
            char SEP = File.separatorChar;
            File dir = new File(System.getProperty("java.home") + SEP
                    + "lib" + SEP + "security");
            file = new File(dir, "jssecacerts");
            if (file.isFile() == false) {
                file = new File(dir, "cacerts");
            }
        }
        System.out.println("Loading KeyStore " + file + "...");
        InputStream in = new FileInputStream(file);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(in, passphrase);
        in.close();
 
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManagerFactory tmf =
                TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        X509TrustManager defaultTrustManager = (X509TrustManager)tmf.getTrustManagers()[0];
        SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
        context.init(null, new TrustManager[] {tm}, null);
        SSLSocketFactory factory = context.getSocketFactory();
 
        System.out.println("Opening connection to " + host + ":" + port + "...");
        SSLSocket socket = (SSLSocket)factory.createSocket(host, port);
        socket.setSoTimeout(10000);
        try {
            System.out.println("Starting SSL handshake...");
            socket.startHandshake();
            socket.close();
            System.out.println();
            System.out.println("No errors, certificate is already trusted");
        } catch (SSLException e) {
            System.out.println();
            e.printStackTrace(System.out);
        }
 
        X509Certificate[] chain = tm.chain;
        if (chain == null) {
            System.out.println("Could not obtain server certificate chain");
            return;
        }
 
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(System.in));
 
        System.out.println();
        System.out.println("Server sent " + chain.length + " certificate(s):");
        System.out.println();
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        for (int i = 0; i < chain.length; i++) {
            X509Certificate cert = chain[i];
            System.out.println
                    (" " + (i + 1) + " Subject " + cert.getSubjectDN());
            System.out.println("   Issuer  " + cert.getIssuerDN());
            sha1.update(cert.getEncoded());
            System.out.println("   sha1    " + toHexString(sha1.digest()));
            md5.update(cert.getEncoded());
            System.out.println("   md5     " + toHexString(md5.digest()));
            System.out.println();
        }
 
        System.out.println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
        String line = reader.readLine().trim();
        int k;
        try {
            k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
        } catch (NumberFormatException e) {
            System.out.println("KeyStore not changed");
            return;
        }
 
        X509Certificate cert = chain[k];
        String alias = host + "-" + (k + 1);
        ks.setCertificateEntry(alias, cert);
 
        OutputStream out = new FileOutputStream("jssecacerts");
        ks.store(out, passphrase);
        out.close();
 
        System.out.println();
        System.out.println(cert);
        System.out.println();
        System.out.println
                ("Added certificate to keystore 'jssecacerts' using alias '"
                        + alias + "'");
    }
 
    private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
 
    private static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 3);
        for (int b : bytes) {
            b &= 0xff;
            sb.append(HEXDIGITS[b >> 4]);
            sb.append(HEXDIGITS[b & 15]);
            sb.append(' ');
        }
        return sb.toString();
    }
 
    private static class SavingTrustManager implements X509TrustManager {
 
        private final X509TrustManager tm;
        private X509Certificate[] chain;
 
        SavingTrustManager(X509TrustManager tm) {
            this.tm = tm;
        }
 
        public X509Certificate[] getAcceptedIssuers() {
            throw new UnsupportedOperationException();
        }
 
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            throw new UnsupportedOperationException();
        }
 
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            this.chain = chain;
            tm.checkServerTrusted(chain, authType);
        }
    }
 
}

D:\桌面\ 。。。\src\main\java\com\ideal\sk>java InstallCert 需要访问的地址:端口

或者设置idea 运行的args参数

执行完后会出现这样的界面

直接输入1,然后会在相应的目录下产生一个名为‘jssecacerts’的证书

将证书copy到$JAVA_HOME/jre/lib/security目录下

重启程序即可解决文章来源地址https://www.toymoban.com/news/detail-776312.html

到了这里,关于报错 unable to find valid certification path to requested target executing的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • sqlmap报错[CRITICAL] unable to connect to the target URL. sqlmap is going to retry the request(s)

    如图,用读文件的方式跑sqlmap时,明明站点是通的,却提示不可达,后来发现目标站点是HTTPS的,sqlmap默认是用http协议 解决方案: 添加代理,让sqlmap走burp的代理,添加ssl 具体命令: 问题解决。

    2024年02月12日
    浏览(50)
  • RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    使用yolov5l模型训练时出现报错,但是昨天使用yolov5s模型时是可以正常训练的。 发生报错的原因是gpu内存占用过高,terminal输入nvidia-smi查看gpu的使用情况。   我们需要把bach_size调小,一般建议是8的倍数,内存不够用时尽量调低,此处我设置成了16。 结果运行正常。 使用yol

    2024年02月11日
    浏览(30)
  • Unable to connect to the server: x509: certificate has expired or is not yet valid

    手动更新所有证书,执行命令 更新用户配置 用更新后的admin.conf替换/root/.kube/config文件 k8s解决证书过期官方文档:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/kubeadm/kubeadm-certs/ 帮助文档: https://www.cnblogs.com/00986014w/p/13095628.html

    2024年02月04日
    浏览(34)
  • 【bug解决】RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    进行深度学习的算法模型训练的时候,终端报错: 产生报错的原因可能有两种: 1.模型训练的环境中cudnn,CUDA的版本号不匹配 解决办法:安装对应的cudnn,以及cuda,找到对应的torch框架,进行安装 2.其实问题更加简单,是模型的训练的batch-size训练过大了,调整更小,就可以了

    2024年02月11日
    浏览(39)
  • K8S异常之Unable to connect to the server: x509: certificate has expired or is not yet valid

    2.1 处理步骤 2.2 处理步骤详细情况 如上,发现很多证书都是 invalid 的状态,接着更新证书: 如下,更新证书后,证书过期时间已经更新为 365d 3.1 再次查看kubectl get node,发现有新的错误: error: You must be logged in to the server (Unauthorized) 3.2 上述错误解决方案 备份配置文件 cp -rp

    2024年02月03日
    浏览(35)
  • Git Clone 报错 `SSL certificate problem: unable to get local issuer certificate`

    如果您在尝试克隆Git存储库时得到 “SSL certificate problem: unable to get local issuer certificate” 的错误,这意味着Git无法验证远程存储库的SSL证书。如果SSL证书是自签名的,或者SSL证书链有问题,就会发生这种情况。 想要修复这个错误,可以尝试以下解决方案: 一、 禁用SSL验证: 一般

    2024年02月07日
    浏览(37)
  • 关于picgo图床报错“unable to verify the first certificate“

    关于picgo图床报错\\\"unable to verify the first certificate\\\" 编程上的疑难杂症(一) 问题:本人picgo加github图床上传出现以下问题 \\\"message\\\": \\\"unable to verify the first certificate\\\"(无法验证第一证书) 问题分析: 图床是github图床,工具是picgo,为了可以顺利访问github用到steam++(Watt Toolkit)加速

    2024年02月11日
    浏览(43)
  • Composer出现 SSL certificate problem: unable to get local issuer certificate 报错的解决方法

    Composer出现crul SSL报错的问题是没有安装CA证书导致的!!! 错误信息如下: [ComposerDownloaderTransportException]                                                                                                      curl error 60 while downloading https://repo.packagist.org/package

    2024年02月03日
    浏览(42)
  • 使用postman时,报错SSL Error: Unable to verify the first certificate

    开发中使用postman调用接口,出现以下问题,在确认路径、参数、请求方式均为正确的情况下 解决方法 File - Settings - SSL certification verification 关闭 找到图中配置,这里默认是打开状态,把它关闭即可:ON - OFF 再次请求接口 原因:使用 Postman 发起 HTTPS 请求时,它会验证服务器的

    2024年02月04日
    浏览(34)
  • .net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure.

    因为Grpc采用HTTP/2作为通信协议,默认采用LTS/SSL加密方式传输,比如使用.net core启动一个 服务端(被调用方) 时:   其中使用UseHttps方法添加证书和秘钥。 但是,有时候,比如开发阶段,我们可能没有证书,或者是一个自己制作的临时测试证书,那么在 客户端(调用方)

    2023年04月13日
    浏览(32)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包