Received fatal alert: handshake_failure

墨蓝 2023-06-15 13:30 89阅读 0赞

项目环境配置
java+jdk1.7+tomcat+eclipse
由于不能升级项目jdk版本,只好想办法兼容jdk1.7版本
开始网上寻找解决办法,尝试了下载jdk1.7补丁包jce包方法https://www.cnblogs.com/1995hxt/p/6185399.html
https://blog.csdn.net/gege87417376/article/details/77936507,还是依然报错,继续尝试其他办法

  1. javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
  2. at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.7.0_80]
  3. at sun.security.ssl.Alerts.getSSLException(Alerts.java:154) ~[na:1.7.0_80]
  4. at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1979) ~[na:1.7.0_80]
  5. at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1086) ~[na:1.7.0_80]

报错代码:

  1. static {
  2. SSLContext sslContext = null;
  3. try {
  4. sslContext = SSLContexts.custom()
  5. .loadTrustMaterial(new TrustStrategy() {
  6. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  7. return true;
  8. }
  9. }).build();
  10. } catch (KeyManagementException e) {
  11. e.printStackTrace();
  12. } catch (NoSuchAlgorithmException e) {
  13. e.printStackTrace();
  14. } catch (KeyStoreException e) {
  15. e.printStackTrace();
  16. }
  17. SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslContext,
  18. new String[] { "TLSv1.2" },
  19. null,
  20. NoopHostnameVerifier.INSTANCE);
  21. System.setProperty("https.protocols", "TLSv1.1,TLSv1.2,SSLv3");
  22. Registry<ConnectionSocketFactory> sfRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
  23. .register("http", PlainConnectionSocketFactory.getSocketFactory())
  24. .register("https", sslSf)
  25. .build();
  26. conMgr = new PoolingHttpClientConnectionManager( sfRegistry );
  27. conMgr.setMaxTotal( MAXCONNECTION );
  28. conMgr.setDefaultMaxPerRoute( MAXCONNECTION );
  29. hb= HttpClients.custom().setSSLContext( sslContext );
  30. hb.setConnectionManager( conMgr );
  31. }

调整好后的代码

  1. static {
  2. SSLContext sslContext = null;
  3. try {
  4. sslContext = new SSLContextBuilder().useProtocol("TLSv1.2")
  5. .loadTrustMaterial(null, new TrustStrategy(){
  6. public boolean isTrusted(X509Certificate[] arg0, String arg1)
  7. throws CertificateException{
  8. return true;
  9. }
  10. }).build();
  11. } catch (KeyManagementException e) {
  12. e.printStackTrace();
  13. } catch (NoSuchAlgorithmException e) {
  14. e.printStackTrace();
  15. } catch (KeyStoreException e) {
  16. e.printStackTrace();
  17. }
  18. javax.net.ssl.HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
  19. SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
  20. Registry<ConnectionSocketFactory> sfRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
  21. .register("http", PlainConnectionSocketFactory.getSocketFactory())
  22. .register("https", sslSocketFactory)
  23. .build();
  24. conMgr = new PoolingHttpClientConnectionManager( sfRegistry );
  25. conMgr.setMaxTotal( MAXCONNECTION );
  26. conMgr.setDefaultMaxPerRoute( MAXCONNECTION );
  27. hb= HttpClients.custom().setSSLContext( sslContext );
  28. hb.setConnectionManager( conMgr );
  29. }

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读