【发布时间】:2019-02-25 15:35:33
【问题描述】:
我正在尝试从 java SpringBoot 应用程序连接到 mySQL RDS DB。我已经对数据库进行了必要的更改。
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);
}
}
}
参考资料:
【问题讨论】:
标签: java mysql ssl amazon-rds mysql-connector