我试图使用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

当前回答

如果你使用的是ubuntu/linux系统,在终端运行下面的命令。

cd /usr/local/share/ca-certificates

Cp /文件位置/----。crt。(拷贝CRT文件到/usr/local/share/ca-certificates)

sudo update-ca-certificates

其他回答

如果您在Spring Tools Suite或Eclipse等IDE中遇到此错误,请首先尝试使用URL的HTTP版本而不是HTTPS版本。

当我试图将SonarQube链接添加到Spring Tools Suite时,我遇到了这个错误。我将我的URL替换为:

http://sonarqube...

而不是

https://sonarqube...

我已经成功连接SonarQube。

我希望它能对那些像我一样在寻找这个问题答案的人有所帮助。

I solved this issue on Windows Server 2016 with Java 8, by importing cert from pkcs12 store to cacerts keystore. Path to pkcs12 store: C:\Apps\pkcs12.pfx Path to Java cacerts: C:\Program Files\Java\jre1.8.0_151\lib\security\cacerts Path to keytool: C:\Program Files\Java\jre1.8.0_151\bin After possitioning to folder with keytool in command prompt (as administrator), command to import cert from pkcs12 to cacerts is as follows: keytool -v -importkeystore -srckeystore C:\Apps\pkcs12.pfx -srcstoretype PKCS12 -destkeystore "C:\Program Files\Java\jre1.8.0_151\lib\security\cacerts" -deststoretype JKS You will be prompted to: 1. enter destination keystore password (cacerts pasword, default is "changeit") 2. enter source keystore password (pkcs12 password)

要使更改生效,请重新启动服务器机器(或只需重新启动JVM)。

我也遇到了同样的问题,我使用的是8.1.0-3,但后来我使用了9.2.1-0,这个问题在没有任何手动步骤的情况下得到了修复。自签名证书工作正常。

加仙人掌对我没用。 在使用flag -Djavax.net.debug=all启用日志后,才知道java从jssecacerts读取。

导入到jssecacerts终于工作了。

问题背景:

当我试图在我的项目中运行mvn清洁安装并通过Netbeans IDE清洁和构建选项时,我得到了以下错误。 此问题是由于通过NET beans IDE/通过命令提示符下载时证书不可用,但可以通过浏览器下载文件。

错误:

Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact com.java.project:product:jar:1.0.32 from/to repo-local (https://url/local-repo): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  

解决方法:

1. 下载有关网址的证书:

Launch IE by "run as adminstrator" (otherwise, we will not be able to download the certificate) Enter the url in IE-> https://url/local-repo (In my case this url had a untrusted certificate.) Download the certificate by clicking on Certificate error -> view certificate Select Details tab -> copy to file -> next -> select "DER encoded binary X.509 (.CER) save the certificate in some location, example : c:/user/sheldon/desktop/product.cer Congrats! you have successfully downloaded the certificate for the site

2. 现在安装密钥存储库以修复该问题。

运行keytool命令将下载的密钥存储库附加到 已存在的证书文件。 命令:jdk (JAVA_HOME) bin文件夹下的如下命令。

C:\Program Files\Java\ jdk1.8.0_141\jre\bin>keytool -importcert -file .使用实例 “C: / user /谢耳朵/桌面/产品。-alias product -keystore “C: /程序文件/ Java / jdk1.8.0_141 / jre / lib /安全/除”。

系统将提示您输入密码。输入keystore密码: 再次输入“changeit”,输入“信任此证书?”(没有):“,输入 “是的”

示例命令行命令/输出:

keytool -importcert -file "C:/Users/sheldon/Desktop/product.cer" -alias product -keystore "C:/Program iles/Java/jdk1.8.0_141/jre/lib/security/cacerts"
Enter keystore password:
Trust this certificate? [no]:  yes
Certificate was added to keystore

Contgrats !现在你应该删除“PKIX路径构建失败:sun.security.provider.certpath”。在Netbeans IDE中出现SunCertPathBuilderException错误。