具有指定证书的 Java MySQL SSL 连接

标签 java mysql ssl jdbc mariadb

我正在尝试制作一个 Java 应用程序,它使用 jdbc (Connector/J) 连接到使用 SSL 加密的 mariadb 数据库。 我已经创建了一个自签名证书并将服务器配置为使用它。 当我尝试连接时出现异常,因为证书不受信任,因为它是自签名的并且未添加到 Java 信任库中。 因为我想修改 TrustStore 或为我的应用程序使用不同的我搜索了一种方法来告诉 jdbc 哪个证书是可信的,并找到了“https://mariadb.com/kb/en/library/using-tlsssl-with-mariadb-connectorj/ 直接提供证书”

当我现在尝试连接时:

DriverManager.getConnection("jdbc:mysql://<hostname>:3306/<database>?useSSL=true&requireSSL=true&serverSslCert=/tmp/ssl/server-cert.pem", "<username>", "<password>");

我得到了这个异常,我在没有再次使用 serverSslCert 参数之前已经得到了这个异常:

Exception in thread "main" 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 136 milliseconds ago.  The last packet sent successfully to the server was 129 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
    at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:203)
    at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4901)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
    at gymcash.server.mysql.DriverWrapper.connect(DriverWrapper.java:22)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at gymcash.server.mysql.MySQL.connect(MySQL.java:48)
    at gymcash.server.mysql.MySQL.connect(MySQL.java:52)
    at gymcash.server.main.Main.main(Main.java:25)
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
    at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:188)
    ... 21 more
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:304)
    at 

sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:992)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596)
    ... 29 more
Caused by: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:154)
    at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:80)
    at java.security.cert.CertPathValidator.validate(CertPathValidator.java:292)
    at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:297)
    ... 31 more

我不明白我做错了什么,因为文件存在,路径是绝对路径,并且与服务器使用的路径相同。有人可以告诉我我做错了什么吗?

我正在使用 Java 8,mysql-connector-java-5.1.45

最佳答案

有几种方法可以做到这一点。一种是将您的自签名证书或您用来签署证书的证书颁发机构放入您用来连接的 JVM 使用的默认 Java keystore 中。您需要使用 Keytool 来执行此操作。但是除非您管理企业 VM 包,否则您可能不想劫持安全包中的默认 keystore 。

首先您需要了解关键工具的工作原理。

[Java Key 工具命令][1] https://www.sslshopper.com/article-most-common-java-keytool-keystore-commands.html

问题是您可能无法访问默认 keystore ,并且它可能不接受您使用的证书格式。要求一些健身房工作转换证书并尝试匹配支持的加密算法。

一般来说,除非你管理一家企业最好只是

  1. 生成一个新的 keystore 文件
  2. 将您的证书导入此 keystore (或证书链)
  3. 确保将其标记为可信
  4. 在运行时使用 -D 选项将 keystore 传递给您的程序。

我通常只是将其添加到我的代码中并在连接到数据库之前设置属性

if (DB_SSL || SSL) {
            System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
            System.setProperty("javax.net.ssl.keyStorePassword", KEYSTORE_PASS);
            System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
            System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASS);
        if (DEBUG_OPTION) {
            System.setProperty("javax.net.debug", "true");
        }
    }

private static String KEYSTORE = "/home/me/.keystore";
private static String KEYSTORE_PASS = "changeit";

关于具有指定证书的 Java MySQL SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244946/

相关文章:

java - 线程中的异常 "AWT-EventQueue-0"java.lang.IllegalArgumentException : input == null

python - 使用 SQLAlchemy 批量更新子查询

ssl - 如果 CURLOPT_SSL_VERIFYPEER 为真,PayPal DoDirectPayment 将因 SSL 而失败

ssl 证书未更新

Java泛型根据值返回枚举

java - Java开关控件中的局部变量

java - 如何使用内连接按页面请求排序

mysql - 如何在rails中获得默认排序规则?

php - 从mysql数据库中的特定用户中选择数据到PHP

java - 如何使用 wireshark 解密服务以服务 SSL 流量?