2

这是我从网上复制的代码

/**
 * A simple example that uses HttpClient to perform a GET using Basic
 * Authentication. Can be run standalone without parameters.
 *
 * You need to have JSSE on your classpath for JDK prior to 1.4
 *
 * @author Michael Becke
 */
public class BasicAuthenticationExample {

    /**
     * Constructor for BasicAuthenticatonExample.
     */
    public BasicAuthenticationExample() {
        super();
    }

    public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();

       client.getState().setCredentials(
                new AuthScope("login.website.com", 443),
                new UsernamePasswordCredentials("login id", "password")
                );

        GetMethod get = new GetMethod(
                "https://url to the file");

        get.setDoAuthentication( true );

        try {
            // execute the GET
            int status = client.executeMethod( get );

            // print the status and response
            System.out.println(status + "\n" + 
                    get.getResponseBodyAsString());

        } finally {
            // release any connection resources used by the method
            get.releaseConnection();
        }
    }
}

现在因为这条线

            int status = client.executeMethod( get );

我收到以下错误

Exception in thread "main" 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 com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:828)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2116)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at BasicAuthenticationExample.main(BasicAuthenticationExample.java:38)
Caused by: 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.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 18 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 24 more

现在这个错误的原因当然是我的服务器发送的证书不在我的受信任证书列表中。我的问题是如何将证书放入受信任的列表中。

谢谢普拉纳贝什

4

3 回答 3

3

此页面似乎回答了您的问题: 将证书添加到您的 Java 密钥库

它引用了 Andres Sterbenz 的 InstallCert 和解释博客文章

如果仅用于测试,您可能不想将证书实际添加到受信任的存储中。所以你也可以禁用证书验证

于 2009-08-13T00:10:57.593 回答
2

您将需要一个包含要信任的证书的密钥库。您可以使用 Java 的keytool命令来执行此操作。有几种方法可以做到这一点:

  • 将其添加到默认 Java 密钥库(.keystore在 Windows 上的主目录或用户目录中)
  • 将系统属性设置javax.net.ssl.trustStore为备用密钥库的位置(类似于java -Djavax.net.ssl.trustStore=/path/to/keystore
  • 构建你自己的SSLSocketFactory实现,可以以任何你想要的方式加载密钥库(这绝对是最复杂的)

密钥库只是一个文件,其中包含可用于各种用途的密钥和/或证书,在这种情况下,是您的应用程序需要信任的证书。Keytool可以按如下方式使用:

keytool -importcert -file /path/to/certificate/file -keystore /path/to/keystore

对于其他命令行选项,只需运行

keytool -help
于 2009-08-13T00:11:38.187 回答
1

您可以使用keytool导入。

于 2009-08-13T00:10:39.343 回答