Java 1.8.0 在 JDBC 连接中启用 TLS1.2

Bem*_*efe 3 java sql-server jdbc

我有一个 SQL Server 2014 更新到最新的修订包 ( 12.0.5207 )。在环境中,唯一启用的协议是 TLS1.2(为此目的设置了注册表项)。我可以使用 SA 帐户在本地和远程使用 Management Studio 连接到 SQL 服务器。

但是,当我尝试使用 java 代码和 JDBC 驱动程序sqljdbc42.jar建立与 SQL 服务器的连接时,会引发以下异常:

驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接。错误:“SQL Server 未返回响应。连接已关闭。

java代码如下:

public static void main(String[] args) 
{
    try 
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    }
    catch (ClassNotFoundException e) 
    {
        System.out.println( e.toString() ); 
    }

    String connectionUrl =  "jdbc:sqlserver://localhost:1433;" +  
                            "databaseName=TRCDB;user=sa;password=**********;";  
    try 
    {
        Connection con = DriverManager.getConnection(connectionUrl);
    } 
    catch (SQLException e) 
    {
        System.out.println( e.toString() ); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

当 JVM 启动时,传递以下选项:

-Djavax.net.debug=all -Djdk.tls.client.protocols="TLSv1.2" -Dhttps.protocols="TLSv1.2"
Run Code Online (Sandbox Code Playgroud)

因此,尽管仅启用了 TLSv1.2,但“Client Hello”是使用 TLSv1 完成的:

jdk.tls.client.protocols is defined as TLSv1.2 SSLv3 protocol was requested but was not enabled 
SUPPORTED: [TLSv1, TLSv1.1, TLSv1.2] 
SERVER_DEFAULT: [TLSv1, TLSv1.1, TLSv1.2] 
CLIENT_DEFAULT: [TLSv1.2] 
...
*** ClientHello, TLSv1
...
main, WRITE: TLSv1 Handshake
...
main, called close()
main, called closeInternal(true)
main, SEND TLSv1.2 ALERT:  warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 2
Run Code Online (Sandbox Code Playgroud)

难道是TLS版本问题的根本原因?如何强制使用TLSv1.2

Gor*_*son 9

Microsoft 用于 SQL Server 的旧版本 JDBC 驱动程序显然假定 TLS v1.1 将在服务器上可用。也就是说,它们没有被编码来处理服务器明确拒绝(或忽略)TLS v1.1 流量的情况。

从 JDBC 驱动程序版本 6.3.2开始,我们可以添加;sslProtocol=TLSv1.2到我们的连接 URL 以指定要使用的 TLS 版本。

  • 使用enabledTLSProtocols连接URL参数对我有用,例如:`jdbc:mysql://<host>:<port>/<db>?enabledTLSProtocols=TLSv1.2` (2认同)