简体   繁体   English

如何修复“驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接”错误

[英]How to fix " The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption" error

I am trying to connect MS SQL server 2016 through java.我正在尝试通过 java 连接 MS SQL Server 2016。 I am using JDK1.8 and mssql-jdbc-7.2.1.jre8.jar file.我正在使用 JDK1.8 和 mssql-jdbc-7.2.1.jre8.jar 文件。

public class ConnectToMSSQL {
    public static void main(String[] args) {
        String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=TestSQL;user=sa;password=password";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection connection = DriverManager.getConnection(connectionURL);
            if (connection != null) {
                System.out.println("Connected");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

While running the above code, got the below error, how to fix this?运行上述代码时,出现以下错误,如何解决?

com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "RSA premaster secret error". ClientConnectionId:8438be77-02cd-4d82-b84d-16c0045ac415
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2887)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2452)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2103)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1950)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1162)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:735)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at database.ConnectToMSSQL.main(ConnectToMSSQL.java:12)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
    at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1799)
    ... 8 more
Caused by: java.security.NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available
    at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
    at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
    at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
    ... 18 more

It is working fine now.它现在工作正常。 Just I have done the following: in Eclipse > Window > Preferences > Java > Installed JREs > removed and added the same JDK path, now it is working fine.我刚刚完成了以下操作:在 Eclipse > Window > Preferences > Java > Installed JREs > 中删除并添加了相同的 JDK 路径,现在它工作正常。

You should use logging. 你应该使用日志记录。 First create a logging property file: 首先创建一个日志属性文件:

handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
.level= FINEST
java.util.logging.ConsoleHandler.level= FINEST
java.util.logging.FileHandler.level= FINEST
java.util.logging.FileHandler.pattern=%t/edpLog%u%g.log
java.util.logging.ConsoleHandler.formatter = java.util.logging.XmlFormatter
java.util.logging.FileHandler.formatter = java.util.logging.XmlFormatter
com.microsoft.sqlserver.jdbc.TDSChannel.level = FINEST
com.microsoft.sqlserver.jdbc.TDSReader.level = FINEST
com.microsoft.sqlserver.jdbc.TDSWriter.level = FINEST
com.microsoft.sqlserver.jdbc.TDS.DATA.level = FINEST

The %t in %t/edpLog%u%g.log means the log file will be saved to your systems temporary folder. %t%t/edpLog%u%g.log意味着日志文件将被保存到你的系统临时文件夹。

Next add the logging class to your class. 接下来将日志记录类添加到您的类。

import  java.util.logging.*;

public class ConnectToMSSQL {

 private static Logger logger;
  static {
      System.setProperty("java.util.logging.config.file",  "C:\\Path\\To\\Log\\Property\\File\\logging.properties");
      logger =  Logger.getLogger(ConnectToMSSQL.class.getName());
   }

    public static void main(String[] args) {
    ....
    }
}

You should get a log file and the output sent to your console. 您应该获得一个日志文件并将输出发送到您的控制台。 It's going to be quite verbose, so good luck. 这将是非常冗长,祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 驱动程序无法使用安全 Sockets 层 (SSL) 加密建立与 SQL 服务器的安全连接。 如何解决这个错误? - The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. How to solve this error? 驱动程序无法使用安全套接字层(SSL)加密与SQL Server建立安全连接 - The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption SQL 服务器 JDBC 错误:驱动程序无法使用安全 Sockets 层 (SSL) 加密与 SQL 服务器建立安全连接 - SQL Server JDBC Error: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption SQL Server JDBC Error on Java 8: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption - SQL Server JDBC Error on Java 8: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption 驱动程序无法使用安全 Sockets 层 (SSL) 加密建立与 SQL 服务器的安全连接。 错误:“意外重新抛出” - The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Unexpected rethrowing" 驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接。 错误:“PKIX 路径构建失败: - The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "PKIX path building failed: 驱动程序无法使用 SSL 与 SQL 服务器建立安全连接 - The driver could not establish a secure connection to SQL Server by using SSL 无法使用 MS JDBC 驱动程序从 CentOS 8 服务器连接到 SQL 服务器服务器:使用安全连接 ZEA52C2Z42253C5F99C23Z 错误 - Cannot connect to SQL Server server with MS JDBC Driver from CentOS 8 server: SSL error but not using secure connection 使用 Java 建立到 MySQL Amazon RDS (SSL/TLS) 的安全连接 - Using Java to establish a secure connection to MySQL Amazon RDS (SSL/TLS) 如何以与驱动程序无关的方式与 DB(SQL Server、PG)建立 SSL 连接? - How to establish SSL connection with DB (SQL Server, PG), in a driver agnostic way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM