gpt4 book ai didi

java - CertPathValidatorException Java 尝试使用 SSL 连接到服务器数据库

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:09 27 4
gpt4 key购买 nike

我有一个 Java 应用程序(带 hibernate ),我试图在一个项目中使用 Google Cloud SQL + Compute Engine,但没有成功。

Google Cloud SQL 要求我使用 server-ca.pemclient-cert.pemclient-key 通过 SSL 连接到数据库。 pem.

在此之前,我使用属性连接到我的数据库:

hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/dbname?autoReconnect=true&useSSL=false
hibernate.connection.username=root
hibernate.connection.password=pass

hibernate.dialect=org.hibernate.dialect.MySQLDialect

如何更改它以使用 SSL?

谢谢!

[编辑]

尝试了 Pooja Aggarwal 的回答,但没有成功。

使用 -Djavax.net.debug=all 我得到异常:

Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

[编辑 2]

我为生成证书而运行的命令:

openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem \
-name "mysqldb" -passout pass:MYPASS -out client-keystore.p12


keytool -importkeystore -srckeystore client-keystore.p12 -srcstoretype pkcs12 \
-srcstorepass MYPASS -destkeystore keystore -deststoretype JKS -deststorepass MYPASS

然后执行:

java -Djavax.net.ssl.keyStore=/home/user/cert/keystore -Djavax.net.ssl.keyStorePassword=MYPASS -jar MyApp.jar

附言:我可以使用以下方式连接:

 mysql -uroot -p -h 192.00.00.00 \
--ssl-ca=server-ca.pem --ssl-cert=client-cert.pem \
--ssl-key=client-key.pem

最佳答案

按照此链接中提到的步骤操作。 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-using-ssl.html

您需要将 useSSL 标记为 true 并在您的 JDK 中导入证书,其步骤在上面的链接中提到。然后你需要设置系统属性。

System.setProperty("javax.net.ssl.keyStore","path_to_keystore_file"); 
System.setProperty("javax.net.ssl.keyStorePassword","mypassword");

使用此链接中提到的步骤导入证书:How to import a .cer certificate into a java keystore?

关于java - CertPathValidatorException Java 尝试使用 SSL 连接到服务器数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52809444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com