gpt4 book ai didi

java - MySQL(谷歌云 SQL): User Access denied only from Java

转载 作者:行者123 更新时间:2023-11-29 05:09:36 25 4
gpt4 key购买 nike

我已经使用 SSL 证书设置了 Google Cloud SQL (MySQL 5.7)。

现在,当我尝试连接 HeidiSQL 时,它可以使用以下设置:

  • 客户证书
  • 服务器证书
  • 客户端 key
  • 网址:本地主机
  • 端口:23306
  • 用户
  • 密码
  • 数据库

我将端口从我的生产服务器转发到端口 23306 上的本地计算机。(DB-Access 仅限于生产服务器的 IP)

现在我尝试使用相同的设置运行我的 spring boot 应用程序。但我总是得到这个错误:

java.sql.SQLException: Access denied for user 'mysqluser'@'x.x.x.x' (using password: YES)

我创建了一个 keystore 和信任库,并将 JVM 配置为通过以下参数使用它们:

-Djavax.net.ssl.keyStore=files/keystore-dev
-Djavax.net.ssl.keyStorePassword=test
-Djavax.net.ssl.trustStore=files/truststore-dev
-Djavax.net.ssl.trustStorePassword=test

我假设这是可行的,因为否则我会在获得 AccessDenied 异常之前得到 SSL-Exception。

我在我的 Spring Boot 应用程序的 application.properties 中使用了以下设置:

spring.datasource.url=jdbc:mysql://localhost:23306/test_db?autoReconnect=true&useSSL=true&requireSSL=true
spring.datasource.username=mysqluser
spring.datasource.password=test
spring.datasource.driverClassName=com.mysql.jdbc.Driver

我将这些设置复制到 HeidiSQL 并且它在那里工作......我不知道我应该尝试解决这个问题。有什么想法吗?

谢谢。

编辑 1:

不,我打开了 SSL 调试并且可以看到以下警告:

Warning: no suitable certificate found - continuing without client authentication

我似乎连接退回到非 SSL 连接,并且我已禁用与 mysql-server 的非 SSL 连接。

但是你知道为什么这不起作用吗?

我使用以下描述从证书生成 keystore 和信任库,并从 google cloud sql 生成 key : https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-using-ssl.html

编辑2这是 SSL 调试的一些输出。会不会是keystore中没有找到key?

keyStore is : files/keystore
keyStore type is : jks
keyStore provider is :
init keystore
init keymanager of type SunX509
trustStore is: files\truststore
trustStore type is : jks
trustStore provider is :
init truststore
adding as trusted cert:
Subject: C=US, O="Google, Inc", CN=Google Cloud SQL Server CA
Issuer: C=US, O="Google, Inc", CN=Google Cloud SQL Server CA
Algorithm: RSA; Serial number: 0x0
Valid from Mon Jan 16 22:54:25 CET 2017 until Wed Jan 16 22:55:25 CET 2019

trigger seeding of SecureRandom
done seeding SecureRandom

最佳答案

问题是生成的 keystore 不正确。

正确的方法:首先,我将 Google Cloud SQL 证书和我的本地 cacerts 文件放在一个文件夹中。

然后我使用以下脚本生成信任和 keystore :

rm generated/*

cp cacerts generated/truststore

# Add Server certificate to truststore:
keytool -import -file server-ca.pem -alias mysqlServerCACert -keystore generated/truststore
# Change password:
keytool -storepasswd -keystore generated/truststore

# Convert key
winpty openssl pkcs12 -export -inkey client-key.pem -in client-cert.pem -name my-key -out generated/client.p12
# Import converted client certificate:
keytool -importkeystore -srckeystore generated/client.p12 -srcstoretype pkcs12 -destkeystore generated/keystore

truststore 和 keystore 将在“generated”文件夹中生成。

关于java - MySQL(谷歌云 SQL): User Access denied only from Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790254/

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