我将SQL Server 2014更新为最新的修订包( 12.0.5207 )。在环境中,唯一启用的协议是TLS1.2(已为此目的设置了注册表项)。我可以使用Management Studio在本地和远程使用SA帐户连接到SQL Server。
但是,当我尝试使用Java代码和JDBC驱动程序 sqljdbc42.jar 建立与SQL Server的连接时,会引发以下异常:
驱动程序无法使用安全套接字层(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() ); } }
启动JVM时,将传递以下选项:
-Djavax.net.debug=all -Djdk.tls.client.protocols="TLSv1.2" -Dhttps.protocols="TLSv1.2"
因此,尽管仅启用了TLSv1.2,但仍使用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
难道是 TLS 版本问题的根本原因?如何强制 TLSv1.2 ?
Microsoft的用于SQL Server的JDBC驱动程序的较早版本显然假定TLS v1.1将在服务器上可用。也就是说,未对它们进行编码以处理服务器明确拒绝(或忽略)TLS v1.1通信的情况。
从JDBC驱动程序版本6.3.2开始,我们可以添加;sslProtocol=TLSv1.2到连接URL中以指定要使用的TLS版本。
;sslProtocol=TLSv1.2