首页 > 其他分享 >Received fatal alert: handshake_failure

Received fatal alert: handshake_failure

时间:2024-01-31 17:58:19浏览次数:19  
标签:Received handshake null String connection https new fatal 请求

背景

从后端请求第三方的提供的https接口,一直提示握手失败javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure,但api放到浏览器直接访问是没问题的,这也证明了人家提供的api可用性。

我发起请求的客户端是jdk8版本,我试着请求其他平台https接口,能获取到数据,又证明了我代码是没问题了。那问题出在哪?

分析

我在本地的IDEA打开SSL的debug调试,在jvm启动加入 

-Djavax.net.debug=all 发起一个请求,携带版本号,加密套件列表,发起第一次握手

 按正常流程,服务器端应该给我回礼Hello,但在日志中却看到握手失败的提示

 这说明服务器那边验证没通过,直接拒绝了本次握手,我们打开分析工具https://myssl.com/,分析下请求的域名

 对方的协议是TLS1.3而我发起的客户端基于jdk8发起的,以下是jdk各版本支持的协议

 jdk8版本并没有提供TLSv1.3的协议支持,所以需要额外的openjsse依赖 可参考该博主 https://blog.csdn.net/devzyh/article/details/122074632

解决

我maven引入该依赖包

    <dependency>
            <groupId>org.openjsse</groupId>
            <artifactId>openjsse</artifactId>
            <version>1.1.13</version>
        </dependency>

对方的加密套件是 TLS_AES_256_GCM_SHA384索性就把我的jdk8小版本升级一波(注意:如果jdk版本比较新,以上问题都不存在,我试过jdk17能正常访问)但若项目不允许大版本升级,那就老老实实的加依赖包吧

改完代码,我们重新发起一个请求

    public static String sendHttpsGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        // 支持TLSv1.3协议的依赖注册到提供者中
        Security.addProvider(new OpenJSSE());
        // 指定请求的协议版本
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContext.getInstance("TLSv1.3");

            X509TrustManager x509TrustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };

            sslcontext.init(null, new TrustManager[]{x509TrustManager}, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());

            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
            // 建立实际的连接
            connection.connect();

            System.out.println("连接成功");
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            /*
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            */
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

查看日志,成功握手,并获取到调用的api接口信息

 若出现提示证书问题,那就需要导入证书,可参考该博文的keytool操作 https://blog.csdn.net/HD243608836/article/details/118705725

标签:Received,handshake,null,String,connection,https,new,fatal,请求
From: https://www.cnblogs.com/dslx/p/17999721

相关文章

  • java报错javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorEx
    解决办法:1、用浏览器登录该https网站,在浏览器地址栏里找到“证书信息”->“详细信息”->“复制到文件”->选择DER编码二进制X.509(.CER)(D)导出证书,如证书名为pro1.cer;2、在jdk的jre/bin包里执行keytool-import-aliaspro1-keystore/usr/java/jdk1.8.0/jre/lib/security/c......
  • 解决 fatal: unable to access 'https://github.com/alibaba/nacos.git/': Failed to
    直接打开这个网站:https://sites.ipaddress.com/github.com/。找到网站中的IP地址复制出里面的IPAddress并粘贴到hosts里面。以下是macos上使用命令行打开方式,也可以直接在硬盘上找到这个文件打开sudovim/private/etc/hosts在最后一行添加如下代码140.82.113.4githu......
  • 从 fatal 错误到 sync.Map:Go中 Map 的并发策略
    从fatal错误到sync.Map:Go中Map的并发策略原创 波罗学 码途漫漫 2024-01-2121:00 发表于上海 听全文为什么Go语言在多个goroutine同时访问和修改同一个map时,会报出fatal错误而不是panic?我们该如何应对map的数据竞争问题呢?码途漫漫踏实地写......
  • 输入git push命令显示ssh: connect to host github.com port 22: Connection timed ou
    之前都好好的,今天 gitpush 的时候突然出现这个问题: 解决方法:更改SSH端口(亲测可用)先试试这个命令:[email protected]此时出现如下提示信息:Theauthenticityofhost'[ssh.github.com]:443([20.205.243.160]:443)'can'tbeestablished.ED25519key......
  • scoop设置代理 , 解决”scoop fatal: unable to access“问题
    报错提示UpdatingScoop...fatal:unabletoaccess'https://github.com/ScoopInstaller/Scoop/':Failedtoconnecttogithub.comport443after21091ms:Couldn'tconnecttoserverRemove-Item:找不到路径“C:\Users\kai\scoop\apps\scoop\new”,因为该路......
  • ssh: connect to host github.com port 22: Operation timed out fatal: Could not re
    问题:git推送到远程仓库发生错误执行命令:gitpushorigindev发生错误:ssh:connecttohostgithub.comport22:Operationtimedoutfatal:Couldnotreadfromremoterepository.Pleasemakesureyouhavethecorrectaccessrightsandtherepositoryexists.......
  • Encountered fatal error while reloading routing: Routing trace file does not mat
      efinity编译在routersetup时候报错Encounteredfatalerrorwhilereloadingrouting:Routingtracefiledoesnotmatchnetlist(netlistnetcount24888v.tracenetcount0).  解决方案:检查客户工程的PNR页面。beneficialskew页面是否打开,如果是on状态,试......
  • python 解决Fatal error in launcher:错误问题
    python解决Fatalerrorinlauncher:错误问题━━━━━━━━━━━━━━━━━━━━━━━━━只要终端用到pip的东西,都在前面加python-m,比如python-mpiplist好了,完美解决!补充知识:python安装exe打包库命令pipinstallpyinstaller报错,使用pip国内镜像解决方法pipi......
  • python -m pip fatal error
    https://zhuanlan.zhihu.com/p/91120727Python中-m的典型用法、原理解析与发展演变解决Windows环境中Python:pip安装出现Fatalerrorinlauncher:Unabletocreateprocessusing'"'问题————————————————版权声明:本文为CSDN博主「专攻数学的文科生」的原创......
  • Sqoop连接数据库报错:Caused by: javax.net.ssl.SSLHandshakeException: No appropriat
    控制台报错:[[email protected]__hadoop-2.0.0-alpha]#./bin/sqooplist-databases--connectjdbc:mysql://192.168.45.10:3306--usernameroot--password1234562023-12-2802:58:50,807WARNtool.BaseSqoopTool:Settingyourpasswordonthecommand-linei......