繁体   English   中英

禁用 mySQL RDS 客户端身份验证

[英]Disable mySQL RDS client authentication

我正在尝试从 java SpringBoot 应用程序连接到 mySQL RDS 数据库。 我已经对数据库进行了必要的更改。

ALTER USER 'my_app'@'%' require SSL;
GRANT USAGE ON *.* TO 'my_app'@'%' REQUIRE SSL;

但是,连接不会发生。 它在客户端身份验证部分失败。 服务器(mysql db)无法对客户端(java 应用程序)进行身份验证。

题:

  • 如何禁用客户端身份验证? 根据文档,除非您执行“REQUIRE X509”,否则应该禁用它。 所以,我对此感到困惑。 我指的文档是这样的: https : //dev.mysql.com/doc/refman/8.0/en/alter-user.html

  • 我怎样才能使用 keyStore 和 trustStore 来完成这项工作?

更多信息:

我还将 AWS 根证书和区域证书导入到我的信任存储中。 此时(没有作为 JVM 传递的 keyStore),连接正常。

但是,在我的应用程序中,我还需要使用 keyStore。

 -Djavax.net.ssl.keyStore=path/keyStore.jks
 -Djavax.net.ssl.keyStorePassword=<keyStore_password>

当我这样做时,连接不会发生并抛出以下错误:

main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: unknown_ca
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

添加 ssl 握手调试我看到以下日志:

*** CertificateRequest
Cert Types: RSA, DSS, ECDSA
Cert Authorities:
<Empty>
localhost-startStop-1, READ: TLSv1.1 Handshake, length = 4
*** ServerHelloDone
matching alias: mysqlrds
matching alias: client-nonprod
*** Certificate chain
chain [0] = [

看起来它在服务器(mysql DB)尝试验证客户端(java应用程序)的部分失败了。 在以下步骤中,客户端(java 应用程序)需要向服务器(mysql db)提供证书。

在上面的步骤 (CertificateRequest) 中,服务器正在发送一个空的“Cert Authorities”。 这通常意味着服务器(mysql db)有一个空的或未初始化的信任库。 之后的步骤(证书链),客户端(java app)向服务器(mysql db)发送一个证书。 在这种情况下,“keyStore_cert.jks”。 但是服务器不知道它,它在握手时抛出错误。

示例代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//1. Dowload my sql connector to a folder "jars". http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar
//   This file and the jars folder are on the same folder (or level). Not inside one another.
//2. edit this file to have your settings. Name of the DB,URL,username,password,trustStore,keyStore,DB name, table name.
//3. compile the file:  javac -cp ".:./jars/mysql-connector-java-8.0.13.jar"  SampleCode.java
//4. run the program:   java -cp ".:./jars/mysql-connector-java-8.0.13.jar"  SampleCode

public class SampleCode {
  public static void main(String[] args) {
    Connection con;
    try {
      // TODO: replace with your settings.
      System.setProperty("javax.net.ssl.trustStore", "path/resource/certs/trustStore.jks");
      System.setProperty("javax.net.ssl.trustStorePassword", "trustStore_pass");
      System.setProperty("javax.net.ssl.keyStore", "path/resource/certs/keyStore.jks");
      System.setProperty("javax.net.ssl.keyStorePassword", "keyStore_pass");
      System.setProperty("javax.net.debug", "ssl:handshake");

      Class.forName("com.mysql.cj.jdbc.Driver");
      // TODO: replace with your settings.
      con = DriverManager.getConnection(
          "jdbc:mysql://my_db_name.random_string.us-east-1.rds.amazonaws.com:3/my_db?useLegacyDatetimeCode=false&verifyServerCertificate=true&useSSL=true&requireSSL=true",
          "my_app", "my_pass");

      System.out.println(" connection successful \n");
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * FROM my_table "); // TODO: replace with yours.
      while (rs.next()) {
        System.out.println("id : " + rs.getString("id"));
      }

      rs.close();
      stmt.close();
      con.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

参考资料:

我在使用休眠的应用程序中遇到了同样的错误。 显然 RDS 仅在 SSL 握手期间进行客户端身份验证。 您可以通过连接属性覆盖用于 jdbc 连接的密钥库,而不是禁用客户端身份验证,因为您需要为应用程序使用单独的密钥库。

  1. 按照https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/ssl-certificate-rotation-mysql.html上的“为 JDBC 更新您的应用程序信任存储”部分中提到的步骤获取将用于RDS连接。
  2. 在示例代码中添加以下代码:
Properties properties = new Properties();
properties.put("user", "my_app");
properties.put("password", "my_pass");  properties.put("clientCertificateKeyStoreUrl","file:path_to_rds_truststore_file");
properties.put("clientCertificateKeyStorePassword","password");
 String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, properties); 

通过执行上述操作,您可以在使用单独的系统范围的密钥库时覆盖将用于建立与 RDS 的连接的密钥库。 请注意,每次想要连接到 RDS 实例时都需要设置这些属性。

正如您的参考文献中提到的

请注意,当一起使用时,连接属性会覆盖其他两种方法设置的值。 此外,使用连接属性设置的任何值仅用于该连接,而使用系统范围值设置的值用于所有连接(除非被连接属性覆盖)。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM