赞
踩
show variables like '%ssl%';
- use mysql;
- #username换成具体的用户
- select ssl_type from user where user='username' and host='%';
- ALTER USER 'username'@'%' REQUIRE SSL;
- FLUSH PRIVILEGES;
1. 找服务端提供三个原始文件
2. 生成相应密钥文件并导入jdk
- # 生成truststore文件,使用密码password123
- keytool -importcert -alias Cacert -file ca.pem -keystore truststoremysql -storepass password123
- # windows下没有openssl,可以找一台linux服务器执行第二个命令生成中间文件client-keystore.p12 使用密码password456,注意这个密码要跟第三部使用的密码一致,不然会出现密码不验证的问题
- openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem -name "mysqlclient" -passout pass:password456 -out client-keystore.p12
- # 回到目标服务器执行第三个命令生成keystore文件,使用密码password456
- keytool -importkeystore -srckeystore client-keystore.p12 -srcstoretype pkcs12 -srcstorepass password456 -destkeystore keystoremysql -deststoretype JKS -deststorepass password456
3. 修改jdbc连接字符串
- public class JDBCMySQL {
-
- public static void main(String[] args) {
- Connection connection = null;
- String urlWithCe = "jdbc:mysql://192.168.1.2:3306/mysql?"
- + "useSSL=true"
- + "&verifyServerCertificate=true"
- + "&requireSSL=true"
- + "&trustCertificateKeyStoreUrl=file:E:\\truststoremysql"
- + "&trustCertificateKeyStorePassword=password123"
- + "&clientCertificateKeyStoreUrl=file:E:\\keystoremysql"
- + "&clientCertificateKeyStorePassword=password456";
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- connection = DriverManager.getConnection(urlWithCe,
- "test", "123456");
- PreparedStatement preparedStatement = connection.prepareStatement("select * from " +
- "acac");
- System.out.println(preparedStatement.executeQuery().first());
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
详细参考
链接https://blog.csdn.net/weixin_42911645/article/details/127070812
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。