Ssl握手失败,无法找到所请求目标的有效证书路径

ztyzrc3y  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(151)

首先,我知道这是问之前,但我已经尝试了解决方案提出,并没有得到任何结果。
我正在尝试用java开发一个简单的程序,它可以连接到一个网站,并从托管在那里的文本文件中读取信息。一开始我认为证书会导致问题,因为我无法在火狐上打开网站而不收到警告。IE浏览器不会产生任何问题。代码如下:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;

public class insig
{
    public static void main(String[] args) throws IOException
    {
        URL fpath           = new URL("website/test.txt");
        BufferedReader br   = new BufferedReader(new InputStreamReader(fpath.openStream()));

        String Reader = null;

        while((Reader = br.readLine()) != null)
        {
            System.out.println(Reader);
        }
    }
}

我首先尝试的是Java: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target上的解决方案,但没有成功。尝试调试,虽然我不是很理解这个问题,但在http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/ReadDebug.html的帮助下,我能够理解一切都是相应的,直到找到一个可信证书。我已经检查了我是否将证书添加到正确的JRE版本中,但仍然有同样的问题。
这个问题可以与网站所有者解决吗?它只是因为这不会只在我的电脑上运行,所以它是不方便配置的每台电脑上再次运行的一切。

knsnq2tg

knsnq2tg1#

这个问题可以与网站所有者解决吗?它只是因为这不会只在我的电脑上运行,所以它是不方便配置的每台电脑上再次运行的一切。
这里没有关于网站所有者的内容,您必须在每台PC上为Java配置证书存储。当然,它不应该是必要的JRE信任存储。它可以是与您的应用程序打包在一起的另一个。在顶部,它可以通过java -Djavax.net.ssl.trustStore(keyStore)参数进行配置。请查看此处:java-ssl-and-cert-keystore
顺便说一句:还有其他选项可以在运行时为您的特定URL设置信任/密钥存储。
已添加:请阅读所有java-and-https-url-connection-without-downloading-certificate
然后决定什么对你有好处。

2izufjch

2izufjch2#

再次,有三种方法:
1.在JRE存储中添加证书链(使用keytool命令可以)。
1.创建您自己存储(同样使用keytool)将其与程序一起提供,并将其设置为-D属性。
1.创建您自己的验证。
如在link中我给你看我的注解:

// Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
         //Vadim: here is the place to check client certs and throw an exception if certs are wrong. When there is nothing all certs accepted. 
        }
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
         //Vadim: here is the place to check server certs and throw an exception if certs are wrong. When there is nothing all certs accepted. 
        }
    } };
    // Install the all-trusting trust manager
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            // Here is the palce to check host name against to certificate owner
            return true;
        }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

    URL url = new URL("https://www.google.com");

如果您将按原样使用它,则不会有任何证书验证-接受所有证书和所有主机。

相关问题