我试图使用twitter4j库为我的java项目获得推文,它在封面下使用java.net.HttpURLConnection(可以在堆栈跟踪中看到)。在我第一次运行时,我得到了一个关于证书sun.security.validator.ValidatorException和sun.security.provider.certpath.SunCertPathBuilderException的错误。然后我添加了twitter证书:

C:\Program Files\Java\jdk1.7.0_45\jre\lib\security>keytool -importcert -trustcacerts -file PathToCert -alias ca_alias -keystore "C:\Program Files\Java\jdk1.7.0_45\jre\lib\security\cacerts"

但是没有成功。以下是获取推文的流程:

public static void main(String[] args) throws TwitterException {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
        .setOAuthConsumerKey("myConsumerKey")
        .setOAuthConsumerSecret("myConsumerSecret")
        .setOAuthAccessToken("myAccessToken")
        .setOAuthAccessTokenSecret("myAccessTokenSecret");
    
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    
    try {
        Query query = new Query("iphone");
        QueryResult result;
        result = twitter.search(query);
        System.out.println("Total amount of tweets: " + result.getTweets().size());
        List<Status> tweets = result.getTweets();
        
        for (Status tweet : tweets) {
            System.out.println("@" + tweet.getUser().getScreenName() + " : " + tweet.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }

这里是错误:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=d35baff5 or
    http://www.google.co.jp/search?q=1446302e
TwitterException{exceptionCode=[d35baff5-1446302e 43208640-747fd158 43208640-747fd158 43208640-747fd158], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.5}
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)
    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
    at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1929)
    at twitter4j.TwitterImpl.search(TwitterImpl.java:306)
    at jku.cc.servlets.TweetsAnalyzer.main(TweetsAnalyzer.java:38)
Caused by: 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 sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:34)
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)
    ... 5 more
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 sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 20 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)
    ... 26 more
Failed to search tweets: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

当前回答

尝试复制java cacerts:

cp /usr/lib/jvm/java - 1.8.0 openjdk 1.8.0.172 b11.fc28——9.。除x86_64 / jre / lib /安全/ $ JAVA_HOME / jre / lib /安全/除

其他回答

这不是twitter特有的答案,但这是在搜索此错误时出现的问题。如果您的系统在连接到一个在web浏览器中查看时似乎具有有效证书的网站时接收到此错误,这可能意味着该网站具有不完整的证书链。

简单总结一下这个问题:证书颁发机构不会使用他们的根证书对任何旧证书进行签名。相反,它们(通常)签署也设置了证书颁发机构标志的中间证书(也就是说,允许签署证书)。然后,当您从CA购买证书时,他们将使用这些中间证书之一签署您的CSR。

您的Java信任存储区很可能只有根证书,而没有中间证书。

错误配置的站点可能只返回已签名的证书。问题:该站点使用的中间证书不在您的信任存储区中。浏览器将通过下载或使用缓存的中间证书来处理这个问题;这最大限度地提高了网站的兼容性。然而,Java和像OpenSSL这样的工具不会。这会导致问题中的错误。

您可以通过使用Qualys SSL测试来验证这种怀疑。如果你在网站上搜索,上面写着

此服务器的证书链不完整。

那就证实了这一点。您还可以通过查看认证路径并查看文本Extra Download来了解这一点。

How to fix it: the server administrator needs to configure the web server to return the intermediate certificates as well. For Comodo, for example, this is where the .ca-bundle file comes in handy. For example, in an Apache configuration with mod_ssl, you'd use the SSLCertificateChainFile configuration setting. For nginx, you need to concatenate the intermediate certificates and the signed certificate and use that in the SSL cert configuration. You can find more by searching for "incomplete certificate chain" online.

在浏览器中进入URL:

点击HTTPS证书链(URL地址旁边的锁图标)。点击“更多信息”>“安全”>“显示证书“>”详细信息“>”导出。拾取名称并选择文件类型example.cer chrome -点击网站图标左侧地址栏,选择“证书”->“详细信息”->“导出”并保存为“Der-encoded binary, single Certificate”格式。

现在您有了带有密钥存储库的文件,并且必须将其添加到JVM中。确定cacerts文件的位置,例如。 C:\Program Files (x86)\Java\ jre1.6.0_22\lib\security\cacerts。 接下来导入示例。在命令行中将Cer文件转换为cacerts(可能需要管理员命令提示符):

"C:\Program Files (x86)\Java\jre1.6.0_22\lib\security\cacerts" -file example.cer

您将被要求输入密码,默认为changeit

重新启动JVM/PC。

来源: http://magicmonster.com/kb/prg/java/ssl/pkix_path_building_failed.html

我们得到上述错误的原因是,JDK与许多受信任的证书颁发机构(CA)证书捆绑在一个名为“cacerts”的文件中,但该文件没有我们自签名证书的任何提示。换句话说,cacerts文件没有导入我们的自签名证书,因此没有将其视为受信任的实体,因此给出了上述错误。

如何修复上述错误

要修复上述错误,只需将自签名证书导入cacerts文件。

首先,找到cacerts文件。我们需要找到JDK的位置。如果您正在通过Eclipse或IntelliJ Idea等IDE运行应用程序,请进入项目设置并找出JDK的位置。 例如,在Mac操作系统上,cacerts文件的典型位置是这个位置/Library/Java/JavaVirtualMachines/ {{JDK_version}}/Contents/Home/jre/lib/security 在windows的机器上,它将在{{Installation_directory}}/{{JDK_version}}/jre/lib/security下

找到cacerts文件后,现在我们需要将自签名证书导入到这个cacerts文件中。如果您不知道如何正确生成自签名证书,请查看上一篇文章。

如果你没有一个证书文件(.crt),只有一个.jks文件,你可以使用下面的命令生成一个.crt文件。如果你已经有了。crt/。Pem文件,那么你可以忽略下面的命令

##从密钥库生成证书。JKS文件)####

keytool -export -keystore keystore.jks -alias selfsigned -file selfsigned.crt

以上步骤将生成一个名为selfsigned.crt的文件。现在将证书导入cacerts

Now add the certificate to JRE/lib/security/cacerts (trustore)
keytool -importcert -file selfsigned.crt -alias selfsigned -keystore {{cacerts path}}

对如

keytool -importcert -file selfsigned.nextgen.crt -alias selfsigned.nextgen -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/security/cacerts

这就是全部,重新启动应用程序,它应该可以正常工作。如果它仍然不工作,并获得SSL握手异常。这可能意味着您正在使用不同的域名,然后在证书中注册。

链接有详细的解释和一步一步的解决方案在这里。

解决这个异常的简单步骤,(我在java 11上做的),

First you need to Download the public SSL certificate file of the target domain that you are trying to call,for that Open the target domain website in the chrome (Example: https://amazonservice.domain.com) Click on the small lock icon before the URL in the browser Click View certificates Now Click on Certificate is valid as shown below Now a new Popup will open as shown below, Open the details tab Select the RootCert at the top Click Export and save the .cer file to your system in a directory

现在您有了目标域的公共密钥存储库,您正在尝试在java应用程序中调用该密钥存储库,现在我们需要将该密钥存储库导入您的jre,为此转到$JAVA_HOME/bin目录并运行以下命令

./keytool -importcert -alias someAliasName -keystore {JAVA_HOME}/lib/security/cacerts -file {PathToDownloads}/certificateFileName.cer

它会要求您输入密码,如果您知道,请输入密码,证书的默认密码是changeit

参考文献

https://confluence.atlassian.com/kb/how-to-import-a-public-ssl-certificate-into-a-jvm-867025849.html https://confluence.atlassian.com/kb/unable-to-connect-to-ssl-services-due-to-pkix-path-building-failed-error-779355358.html

错误

A problem occurred configuring root project 'uy-android-pagos'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.goo  gle.firebase:firebase-crashlytics-gradle:2.1.0.
     Required by:
         project :
      > Could not resolve com.goo  gle.firebase:firebase-crashlytics-gradle:2.1.0.
         > Could not get resource 'https://depdes.artifactory.prod.cloud.ihf/artifactory/itau-oq6-frameworks-maven/com/goo%20%20gle/firebase/firebase-crashlytics-gradle/2.1.0/firebase-crashlytics-gradle-2.1.0.pom'.
            > Could not GET 'https://depdes.artifactory.prod.cloud.ihf/artifactory/itau-oq6-frameworks-maven/com/goo%20%20gle/firebase/firebase-crashlytics-gradle/2.1.0/firebase-crashlytics-gradle-2.1.0.pom'.
               > PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

解决方案

请记住,有许多可能的原因产生这个错误,在我的情况下,我用以下方式修复了它:

用Gradle文件同步项目 检查错误无法解决com。咕gle.firebase: firebase-crashlytics-gradle:魅惑。 结构变化。Gradle (:project) from comGoo gle.firebase:firebase-crashlytic -gradle:2.1.0 to com.google.firebase:firebase-crashlytic -gradle:2.1.0(注意空格)

依赖类路径中的任何错误都会生成这种类型的异常。检查生成问题的提交的gradle。

GL