当前位置:   article > 正文

异常:javax.net.ssl.SSLException: Received fatal alert: protocol_version

javax.net.ssl.sslexception: received fatal alert: protocol_version

异常信息

  1. javax.net.ssl.SSLException: Received fatal alert: protocol_version
  2. at sun.security.ssl.Alerts.getSSLException(Unknown Source)
  3. at sun.security.ssl.Alerts.getSSLException(Unknown Source)
  4. at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
  5. at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
  6. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  7. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  8. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  9. at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
  10. at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
  11. at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
  12. at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
  13. at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
  14. at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
  15. at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)

异常原因

先在代码中加入 System.setProperty("javax.net.debug", "all")或设置VM参数-Djavax.net.debug=all,以显示https握手过程,方便调试:

  1. trigger seeding of SecureRandom
  2. done seeding SecureRandom
  3. Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  4. Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  5. Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  6. Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
  7. Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
  8. Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  9. Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  10. Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  11. Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  12. Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  13. Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
  14. Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  15. Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  16. Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  17. Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
  18. Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  19. Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  20. Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  21. Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  22. Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA
  23. Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256
  24. Allow unsafe renegotiation: false
  25. Allow legacy hello messages: true
  26. Is initial handshake: true
  27. Is secure renegotiation: false
  28. AWT-EventQueue-0, setSoTimeout(3000) called
  29. %% No cached client session
  30. *** ClientHello, TLSv1
  31. RandomCookie: GMT: 1530541852 bytes = { 25, 106, 142, 179, 195, 87, 163, 223, 105, 170, 57, 91, 102, 15, 218, 48, 52, 167, 231, 83, 190, 177, 54, 27, 232, 111, 11, 140 }
  32. Session ID: {}
  33. Cipher Suites: [TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_RC4_128_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA]
  34. Compression Methods: { 0 }
  35. Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
  36. Extension ec_point_formats, formats: [uncompressed]
  37. Extension server_name, server_name: [host_name: github.com]
  38. ***
  39. [write] MD5 and SHA1 hashes: len = 168
  40. 0000: 01 00 00 A4 03 01 5B 3A 37 1C 19 6A 8E B3 C3 57 ......[:7..j...W
  41. 0010: A3 DF 69 AA 39 5B 66 0F DA 30 34 A7 E7 53 BE B1 ..i.9[f..04..S..
  42. 0020: 36 1B E8 6F 0B 8C 00 00 2A 00 33 C0 04 00 16 00 6..o....*.3.....
  43. 0030: 05 C0 03 C0 11 C0 02 C0 07 C0 13 C0 08 C0 0C 00 ................
  44. 0040: FF C0 0D C0 0E C0 09 00 2F C0 12 00 04 00 32 00 ......../.....2.
  45. 0050: 13 00 0A 01 00 00 51 00 0A 00 34 00 32 00 17 00 ......Q...4.2...
  46. 0060: 01 00 03 00 13 00 15 00 06 00 07 00 09 00 0A 00 ................
  47. 0070: 18 00 0B 00 0C 00 19 00 0D 00 0E 00 0F 00 10 00 ................
  48. 0080: 11 00 02 00 12 00 04 00 05 00 14 00 08 00 16 00 ................
  49. 0090: 0B 00 02 01 00 00 00 00 0F 00 0D 00 00 0A 67 69 ..............gi
  50. 00A0: 74 68 75 62 2E 63 6F 6D thub.com
  51. AWT-EventQueue-0, WRITE: TLSv1 Handshake, length = 168
  52. [Raw write]: length = 173
  53. 0000: 16 03 01 00 A8 01 00 00 A4 03 01 5B 3A 37 1C 19 ...........[:7..
  54. 0010: 6A 8E B3 C3 57 A3 DF 69 AA 39 5B 66 0F DA 30 34 j...W..i.9[f..04
  55. 0020: A7 E7 53 BE B1 36 1B E8 6F 0B 8C 00 00 2A 00 33 ..S..6..o....*.3
  56. 0030: C0 04 00 16 00 05 C0 03 C0 11 C0 02 C0 07 C0 13 ................
  57. 0040: C0 08 C0 0C 00 FF C0 0D C0 0E C0 09 00 2F C0 12 ............./..
  58. 0050: 00 04 00 32 00 13 00 0A 01 00 00 51 00 0A 00 34 ...2.......Q...4
  59. 0060: 00 32 00 17 00 01 00 03 00 13 00 15 00 06 00 07 .2..............
  60. 0070: 00 09 00 0A 00 18 00 0B 00 0C 00 19 00 0D 00 0E ................
  61. 0080: 00 0F 00 10 00 11 00 02 00 12 00 04 00 05 00 14 ................
  62. 0090: 00 08 00 16 00 0B 00 02 01 00 00 00 00 0F 00 0D ................
  63. 00A0: 00 00 0A 67 69 74 68 75 62 2E 63 6F 6D ...github.com
  64. [Raw read]: length = 5
  65. 0000: 15 03 01 00 02 .....
  66. [Raw read]: length = 2
  67. 0000: 02 46 .F
  68. AWT-EventQueue-0, READ: TLSv1 Alert, length = 2
  69. AWT-EventQueue-0, RECV TLSv1 ALERT: fatal, protocol_version
  70. AWT-EventQueue-0, called closeSocket()
  71. AWT-EventQueue-0, handling exception: javax.net.ssl.SSLException: Received fatal alert: protocol_version
  72. AWT-EventQueue-0, called close()
  73. AWT-EventQueue-0, called closeInternal(true)

由上边握手日志可知,客户端使用的是TLSv1协议,查看JDK版本是1.7,根据Oracle文档知各版本JDK默认使用的TLS协议如下:

 JDK 8
(March 2014 to present)
JDK 7
(July 2011 to present)
JDK 6
(2006 to end of public updates 2013)
TLS ProtocolsTLSv1.2 (default)
TLSv1.1
TLSv1
SSLv3
TLSv1.2
TLSv1.1
TLSv1 (default)
SSLv3

TLS v1.1 (JDK 6 update 111 and above)
TLSv1 (default)
SSLv3
JSSE Ciphers: Ciphers in JDK 8 Ciphers in JDK 7 Ciphers in JDK 6
Reference:JDK 8 JSSEJDK 7 JSSEJDK 6 JSSE
Java Cryptography Extension, Unlimited Strength (explained later)JCE for JDK 8JCE for JDK 7JCE for JDK 6

通过火狐浏览器打开该网页,发现其使用的TLS协议是TLSv1.2:


解决办法

知道了原因以后就好找解决方法了:

1:把JDK升级到1.8。由于该网站使用的是TLSv1.2协议,JDK1.8默认是该协议,故把客户端JDK升级到1.8可以解决该问题。

2:在JDK1.7客户端代码中指定使用的协议System.setProperty("https.protocols", "TLSv1.2")

System.setProperty("jdk.tls.client.protocols", "TLSv1.2")。注意,该方法有时候会莫名的失效,原因未知。

3:在VM参数中设置-Dhttps.protocols=TLSv1.2-Djdk.tls.client.protocols=TLSv1.2。注意,该方法有时候会莫名的失效,原因未知。

4:使用第三方库了,参考http://ligaosong.iteye.com/blog/2356346

5:使用httpclient,其支持配置使用指定协议

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/118264
推荐阅读
相关标签
  

闽ICP备14008679号