似乎有一堆關於類似問題的帖子,但是我找不到任何對我有用的東西。只有在生產服務器上的Java HttpClient SSL握手例外
我有Java 7中的代碼,使用HttpClient 4.3.5來獲取特定的URL。當我通過mvn測試在本地運行或部署到tomcat服務器時,此代碼有效,但在生產服務器上的tomcat上運行時出現SSL握手異常失敗。我將在下面包含正在使用的代碼(請忽略一些httpclient設置完全不安全的情況,即使它設置不正確,它仍然用於處理任何站點和證書),並使用-Djavax跟蹤錯誤的堆棧跟蹤。 net.debug =全部包含在tomcat運行時中以獲取更多日誌記錄細節。我已經實施了Java加密無限強度管轄權政策擴展,並且還添加了彈性城堡,允許我的代碼適用於大多數網站。目前,它似乎只適用於所有這些服務器,除了一個只在生產服務器上出現錯誤但在本地適合我的網站。
我的代碼:
Security.insertProviderAt(new BouncyCastleProvider(), 1);
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
SSLContextBuilder sslBuilder = new SSLContextBuilder();
sslBuilder.loadTrustMaterial(trustStore, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build(), new String[] { "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
PlainConnectionSocketFactory pcsf = new PlainConnectionSocketFactory();
Registry socketFactoryRegistry = RegistryBuilder.create().register("https", sslsf).register("http", pcsf).build();
multiConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
multiConnectionManager.setMaxTotal(100);
multiConnectionManager.setDefaultMaxPerRoute(10);
httpClientBuilder.setConnectionManager(multiConnectionManager);
this.httpClient = httpClientBuilder.disableContentCompression().setSSLSocketFactory(sslsf).build();
HttpGet call = new HttpGet(url);
HttpResponse getResponse;
try {
call.addHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
call.addHeader("Accept","text/html");
call.setConfig(RequestConfig.custom().setCircularRedirectsAllowed(true).setMaxRedirects(5).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build());
try {
getResponse = httpClient.execute(call);
}
catch (Exception ex) {}
}
catch (Exception ex) {}
堆棧跟蹤:
trigger seeding of SecureRandom
done seeding SecureRandom
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256
Allow unsafe renegotiation: false
Allow legacy hello messages: true
Is initial handshake: true
Is secure renegotiation: false
Ignoring disabled protocol: SSLv3
%% No cached client session
*** ClientHello, TLSv1.2
RandomCookie: GMT: 1470874697 bytes = { 159, 94, 240, 233, 124, 91, 106, 83, 249, 126, 156, 56, 200, 67, 114, 18, 205, 36, 55, 140, 229, 223, 66, 190, 204, 226, 223, 90 }
Session ID: {}
Cipher Suites: [TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_DSS_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
Compression Methods: { 0 }
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}
Extension ec_point_formats, formats: [uncompressed]
Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA224withECDSA, SHA224withRSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA, MD5withRSA
***
[write] MD5 and SHA1 hashes: len = 181
0000: 01 00 00 B1 03 03 58 AC C4 49 9F 5E F0 E9 7C 5B ......X..I.^...[
0010: 6A 53 F9 7E 9C 38 C8 43 72 12 CD 24 37 8C E5 DF jS...8.Cr..$7...
0020: 42 BE CC E2 DF 5A 00 00 2C C0 0A C0 14 00 35 C0 B....Z..,.....5.
0030: 05 C0 0F 00 39 00 38 C0 09 C0 13 00 2F C0 04 C0 ....9.8...../...
0040: 0E 00 33 00 32 C0 08 C0 12 00 0A C0 03 C0 0D 00 ..3.2...........
0050: 16 00 13 00 FF 01 00 00 5C 00 0A 00 34 00 32 00 ........\...4.2.
0060: 17 00 01 00 03 00 13 00 15 00 06 00 07 00 09 00 ................
0070: 0A 00 18 00 0B 00 0C 00 19 00 0D 00 0E 00 0F 00 ................
0080: 10 00 11 00 02 00 12 00 04 00 05 00 14 00 08 00 ................
0090: 16 00 0B 00 02 01 00 00 0D 00 1A 00 18 06 03 06 ................
00A0: 01 05 03 05 01 04 03 04 01 03 03 03 01 02 03 02 ................
00B0: 01 02 02 01 01 .....
ActiveMQ Session Task-2, WRITE: TLSv1.2 Handshake, length = 181
[Raw write]: length = 186
0000: 16 03 03 00 B5 01 00 00 B1 03 03 58 AC C4 49 9F ...........X..I.
0010: 5E F0 E9 7C 5B 6A 53 F9 7E 9C 38 C8 43 72 12 CD ^...[jS...8.Cr..
0020: 24 37 8C E5 DF 42 BE CC E2 DF 5A 00 00 2C C0 0A $7...B....Z..,..
0030: C0 14 00 35 C0 05 C0 0F 00 39 00 38 C0 09 C0 13 ...5.....9.8....
0040: 00 2F C0 04 C0 0E 00 33 00 32 C0 08 C0 12 00 0A ./.....3.2......
0050: C0 03 C0 0D 00 16 00 13 00 FF 01 00 00 5C 00 0A .............\..
0060: 00 34 00 32 00 17 00 01 00 03 00 13 00 15 00 06 .4.2............
0070: 00 07 00 09 00 0A 00 18 00 0B 00 0C 00 19 00 0D ................
0080: 00 0E 00 0F 00 10 00 11 00 02 00 12 00 04 00 05 ................
0090: 00 14 00 08 00 16 00 0B 00 02 01 00 00 0D 00 1A ................
00A0: 00 18 06 03 06 01 05 03 05 01 04 03 04 01 03 03 ................
00B0: 03 01 02 03 02 01 02 02 01 01 ..........
[Raw read]: length = 5
0000: 15 03 03 00 02 .....
[Raw read]: length = 2
0000: 02 28 .(
ActiveMQ Session Task-2, READ: TLSv1.2 Alert, length = 2
ActiveMQ Session Task-2, RECV TLSv1 ALERT: fatal, handshake_failure
ActiveMQ Session Task-2, called closeSocket()
ActiveMQ Session Task-2, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
任何幫助,有人能提供解決爲什麼這個握手錯誤發生,爲什麼它是隻發生在生產服務器上(Linux操作系統中運行通過tomcat的java代碼),並在我本地工作正常(mac運行單元測試通過mvn或java代碼通過tomcat)將是最有幫助的。
服務器不喜歡你的問候;有大量的可能性,但**明顯的可能性不是ServerNameIndication **又名SNI。檢查你的工作(本地)案件,看看你是否發送SNI;如果是這樣,但prod不是,請重新檢查您正在運行的兩個HttpClient(特別是如果可能存在多個HttpClient)。也許還可以檢查Java版本;我不記得7內有任何改變,但我很容易忘記。 –
@ dave_thompson_085我已經驗證了兩個版本的java都運行在同一個版本上。我將嘗試驗證是否正在使用相同版本的HttpClient,並且未被tomcat或其他東西覆蓋,最後,如何驗證是否在本地情況下發送SNI?日誌看起來幾乎與我上面發佈的日誌完全相同,除了沒有成功並繼續。如果能幫助解決問題,我可以在本地包含成功的完整日誌。 – Ben
如果正在發送SNI,它將顯示在ClientHello的跟蹤中,作爲「擴展server_name,...」 - 請參閱http://stackoverflow.com/questions/32341729/apache-http-client-defaults-dont-work -with-sni作爲例子(通過一個確實存在一個使用了不想要的舊版本的httpclient的問題的人)。 –