java - 错误 javax.net.ssl.SSLHandshakeException : Received fatal alert: handshake_failure

标签 java certificate ssl-certificate resttemplate client-certificates

我正在尝试使用 Spring RestTemplate 调用 POST Rest 调用:

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

RestTemplate restTemplate = new RestTemplate(requestFactory);

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_XML);

HttpEntity<GetBalanceHistoryRequest> request1 = new     HttpEntity<GetBalanceHistoryRequest>(request, headers);
String result = restTemplate.postForObject("https://server.com/getBalance", request1, String.class);

https://server.com有证书:webapi.tartu-x86.p12 我将证书导入到 C:\Java_8\jre\lib\security\cacerts usinf keytool

运行我的代码后出现以下错误:

SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
trustStore is: C:\Java_8\jre\lib\security\cacerts
trustStore type is : jks
trustStore provider is : 
init truststore
adding as trusted cert:
  Subject: CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US
  Issuer:  CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US
  Algorithm: RSA; Serial number: 0xc3517
  Valid from Mon Jun 21 07:00:00 IDT 1999 until Mon Jun 22 07:00:00 IDT 2020

adding as trusted cert:
  Subject: CN=SecureTrust CA, O=SecureTrust Corporation, C=US
  Issuer:  CN=SecureTrust CA, O=SecureTrust Corporation, C=US
....
....

** Finished
verify_data:  { 31, 64, 180, 145, 192, 1, 180, 119, 86, 70, 247, 140 }
***
[write] MD5 and SHA1 hashes:  len = 16
0000: 14 00 00 0C 1F 40 B4 91   C0 01 B4 77 56 46 F7 8C  .....@.....wVF..
Padded plaintext before ENCRYPTION:  len = 48
0000: 14 00 00 0C 1F 40 B4 91   C0 01 B4 77 56 46 F7 8C  .....@.....wVF..
0010: 3F 56 B1 14 65 F3 18 C6   B3 98 D3 50 65 AC 74 1A  ?V..e......Pe.t.
0020: 48 11 50 C0 0B 0B 0B 0B   0B 0B 0B 0B 0B 0B 0B 0B  H.P.............
main, WRITE: TLSv1 Handshake, length = 48
[Raw write]: length = 53
0000: 16 03 01 00 30 B6 A0 43   3D 91 3A C1 F6 34 F5 73  ....0..C=.:..4.s
0010: 54 A7 1A 46 84 42 1A DC   0D 4D B9 4A C1 3F CB A6  T..F.B...M.J.?..
0020: 57 C6 5D DF C4 1D 62 22   92 FB 1F 3E F1 05 0C 5C  W.]...b"...>...\
0030: 56 9E 9B 02 2D                                     V...-
[Raw read]: length = 5
0000: 15 03 01 00 02                                     .....
[Raw read]: length = 2
0000: 02 28                                              .(
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1 ALERT:  fatal, handshake_failure
%% Invalidated:  [Session-1, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA]
main, called closeSocket()
main, handling exception: javax.net.ssl.SSLHandshakeException: Received                 fatal alert: handshake_failure
 main, called close()
 main, called closeInternal(true)

我正在使用 Java 1.8.0_91

有人可以帮忙吗?

最佳答案

handshake_failure 等 java SSL 问题的原因通常是这些:

  • 不兼容的密码套件:客户端必须使用服务器启用的密码套件
  • 不兼容的 SSL/TLS 版本:客户端必须确保它使用兼容的版本。例如,服务器可能会强制在 java7 中默认未启用的 TLS1.2(不是你的情况)
  • 服务器证书的信任路径不完整:客户端可能不信任服务器证书。通常解决方法是将服务器证书链导入客户端信任库。
  • 错误的服务器配置,例如颁发给不同域的证书或证书链不完整。如果修复在服务器部分

在您的情况下,当 java8 默认使用 TLSv1.2 时,似乎已选择 TLSv1,因此服务器可能未更新为最新版本。

我建议调试协议(protocol)消息 ClientHello 和 ServerHello,以观察套件中选定的协议(protocol)和密码。

-Djavax.net.debug=ssl

您还可以在SSLLabs 中检查服务器的状态。

关于java - 错误 javax.net.ssl.SSLHandshakeException : Received fatal alert: handshake_failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679312/

相关文章:

java - keytool错误: java. io.IOException: keystore 密码不正确

certificate - 邮件服务器上的 SSL 证书是否会降低电子邮件被分类为垃圾邮件的可能性?

security - 如何正确分配根CA?

java - 手动或通过 GUI Builder 设计 GUI

Java:从列表选择监听器获取结果

ssl - 我需要 www 和非 www 的 SSL 证书吗?

android - 如何信任我的应用程序中的所有自签名证书

tomcat - 浏览器用于向服务器发送请求的 SSL 协议(protocol)

java - 我想根据测试用例ID而不是测试用例摘要执行我的测试用例

java - 从 Tika 获取 Word 和 PDF 的段落计数