java 没有可用的 X509TrustManager 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39650898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:32:46  来源:igfitidea点击:

No X509TrustManager Implementation available

javaamazon-web-servicesssl

提问by yogsma

I am invoking an AWS API, I keep running into following error

我正在调用 AWS API,但一直遇到以下错误

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to execute HTTP request: java.security.cert.CertificateException: No X509TrustManager implementation available
       at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:709)
       at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:449)
       at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:411)
       at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:360)
       at com.amazonaws.services.kms.AWSKMSClient.doInvoke(AWSKMSClient.java:2723)
       at com.amazonaws.services.kms.AWSKMSClient.invoke(AWSKMSClient.java:2693)
       at com.amazonaws.services.kms.AWSKMSClient.generateDataKey(AWSKMSClient.java:1488)
       at com.infor.aws.KMSTest.createDEK(KMSTest.java:217)
       at com.infor.aws.KMSTest.main(KMSTest.java:144)
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available
       at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
       at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
       at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
       at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
       at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
       at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
       at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
       at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
       at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
       at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
       at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
       at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
       at com.amazonaws.http.conn.ssl.SdkTLSSocketFactory.connectSocket(SdkTLSSocketFactory.java:132)
       at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
       at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:76)
       at com.amazonaws.http.conn.$Proxy2.connect(Unknown Source)
       at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
       at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
       at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
       at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
       at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
       at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
       at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
       at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:854)
       at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:697)
       ... 8 more
Caused by: java.security.cert.CertificateException: No X509TrustManager implementation available
       at sun.security.ssl.DummyX509TrustManager.checkServerTrusted(SSLContextImpl.java:1119)
       at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
       ... 35 more

I have gone through multiple threads here on Stackoverflow and added all the required truststore, truststoretype system properties. This threadI have tried. But nothing is working. Has anyone come across this ? I am invoking it like below

我已经在 Stackoverflow 上浏览了多个线程,并添加了所有必需的信任库、信任库类型系统属性。这个线程我试过了。但没有任何效果。有没有人遇到过这个?我像下面那样调用它

TrustManagerFactory tmf = TrustManagerFactory
                                   .getInstance(TrustManagerFactory.getDefaultAlgorithm());

KeyStore ks  = KeyStore.getInstance("BCFKS");
ks.load(new FileInputStream("<jre-home>\\lib\\security\\cacerts"), "changeit".toCharArray());
tmf.init(ks);

 // Get hold of the trust manager
X509TrustManager x509Tm = null;
for (TrustManager tm : tmf.getTrustManagers()) {
     if (tm instanceof X509TrustManager) {
          x509Tm = (X509TrustManager) tm;
          break;
     }
}

采纳答案by yogsma

This was resolved when we moved the AWS-client initialization to a place after setting the trust store password. In my code, I was creating AWS-Client too early.

当我们在设置信任存储密码后将 AWS 客户端初始化移动到一个地方时,这个问题得到了解决。在我的代码中,我过早地创建了 AWS-Client。

回答by ramysram

After debuggging for several hours, it turned out to be a corrupted keystore in my case. Check if you can list the contents of the keystore using the keytool command.

在调试了几个小时后,我发现它是一个损坏的密钥库。检查是否可以使用 keytool 命令列出密钥库的内容。