• 日常 - HttpURLConnection 网络请求 TLS 1.2



    环境

    JDK 8
    Hutool 4.5.1

    前言

    应供应商 DD 的 TLS 版本升级通知,企业版接口升级后 TLS 1.0 及 1.1 版本请求将无法连接,仅支持 TLS 1.2 及以上版本的客户端发起请求。

    当前项目使用 Hutool 的 HttpUtil 作为客户端,而该客户端本质就是对 JDK HttpURLConnection 的封装,因此,也就是要判断 JDK 8 是否支持 TLS 1.2。

    HTTPS 请求流程

    Client Server https 请求 https://www.baidu.com/ TCP 三次握手 SYN SYN + ACK ACK 服务端 TLS 信息 Client Hello(发送客户端随机数、支持的 TLS 版本、加密算法) Server Hello(服务端随机数、选定的密钥等) Certificate(网站证书 public key) Server Hello Done(服务端消息发送完毕) 客户端证书验证 客户端 TLS 信息 Client Key Exchange(提供客户端密钥交换算法) Change Cipher Spec(切换加密模式) Finished Change Cipher Spec Finished TLS 验证完毕,发送加密应用数据 Client Server

    服务端支持

    要判断服务端是否支持 TLS 1.2,最简单的方法就是使用浏览器请求下,然后在开发人员工具上查找其安全连接信息,
    百度 TLS 1.2
    可以看到 https://www.baidu.com 已经支持 TLS 1.2,接下来就该验证 JDK 8 是否支持 TLS 1.2 了。

    JDK 验证

    首先判断下 JDK 8 支持哪些 HTTPS 的加密协议:

    try (SSLServerSocket serverSocket = (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket()) {
        System.out.println("服务器支持的协议");
        for (String protocol : serverSocket.getSupportedProtocols()) {
            System.out.println(protocol);
        }
        System.out.println("服务器启用协议");
        for (String protocol : serverSocket.getEnabledProtocols()) {
            System.out.println(protocol);
        }
    }
    System.out.println();
    try (SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket()) {
        System.out.println("客户端支持的协议");
        for (String protocol : socket.getSupportedProtocols()) {
            System.out.println(protocol);
        }
        System.out.println("客户端启用协议");
        for (String protocol : socket.getEnabledProtocols()) {
            System.out.println(protocol);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    输出:

    服务器支持的协议
    TLSv1.3
    TLSv1.2
    TLSv1.1
    TLSv1
    SSLv3
    SSLv2Hello
    服务器启用协议
    TLSv1.3
    TLSv1.2
    SSLv2Hello
    
    客户端支持的协议
    TLSv1.3
    TLSv1.2
    TLSv1.1
    TLSv1
    SSLv3
    SSLv2Hello
    客户端启用协议
    TLSv1.3
    TLSv1.2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    可以看到 JDK 8 客户端是启用了 TLSv1.2 的,但如果发送 HTTPS 请求,那 JDK 8 是否会使用呢?

    接下来初始化好项目,然后在 HttpRequest#execute#846 断点并执行代码:

    String html = HttpUtil.get("https://www.baidu.com");
    System.out.println(html);
    
    • 1
    • 2

    HttpRequest#execute#846

    httpResponse:                          # HttpResponse
      httpConnection:                      # HttpConnection
        conn:                              # HttpURLConnection
          delegate:                        # DelegateHttpsURLConnection
            inputStream:                   # HttpURLConnection$HttpInputStream
              in:                          # ChunkedInputStream
                hc:                        # HttpsClient
                  session:                 # SSLSessionImpl
                    protocolVersion:       # ProtocolVersion
                      name: "TLSv1.2"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    百度 HTML

    就可以确定如果服务端启用 TLS 1.2,那么 JDK 8 是可以支持的了。

    资源

    JDK 8
    百度百科 HTTPS
    dromara/hutool - 🍬A set of tools that keep Java sweet.
    http客户端(Hutool-http)

  • 相关阅读:
    工程伦理--13.3 “邻避效应”中的社会公平问题
    exit()函数、_exit()函数 和 _Exit()函数
    PHP 同城服务共享茶室软硬件结合小程序开发的注意事项?
    实时营销引擎在vivo营销自动化中的实践 | 引擎篇04
    QT串口助手-ZUA课设
    FPGA project : uart232_ram_vga
    Android插件化学习之初识类加载机制
    SpringBoot 自定义参数校验(5)
    js基础笔记学习52-练习2计算水仙花数1
    python使用pandas中的read_csv函数读取csv数据为dataframe、使用map函数和strip函数去除指定字符串数据列的左右空格
  • 原文地址:https://blog.csdn.net/xianzhan_/article/details/130876071