使用Java与MySQL服务器建立SSL连接 [英] SSL connection to MySQL server with Java
问题描述
我正在尝试使用基于SSL的Java连接到MySQL服务器。我收到以下异常:
I am trying to connect to a MySQL server using Java over SSL. I am getting the following exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot connect to MySQL server on www.mysite.com:3306.
Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:827)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException: The method shutdownInput() is not supported in SSLSocket
at sun.security.ssl.BaseSSLSocketImpl.shutdownInput(Unknown Source)
at com.mysql.jdbc.MysqlIO.forceClose(MysqlIO.java:506)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2404)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
... 25 more
这是我的代码:
properties = new Properties();
properties.put("user", "myuser");
properties.put("password", "mypassword");
properties.put("useSSL", "true");
System.setProperty("javax.net.ssl.keyStore", "keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "keystorepass");
System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "truststorepass");
connection = DriverManager.getConnection("jdbc:mysql://www.mysite.com:3306/mydatabase", properties);
详细信息
密钥库和信任库创建
我按如下方式生成了密钥库和信任库:
Details
Keystore and truststore creation
I generated the keystore and truststore as follows:
openssl pkcs12 -export -inkey client-key.pem -in client-cert.pem > keystore.p12
keytool -importkeystore -srckeystore keystore.p12 -srcstoretype pkcs12 -destkeystore keystore.jks -deststoretype JKS
openssl x509 -outform der -in ca.pem -out ca.der
keytool -import -file ca.der -alias myCA -keystore truststore.jks
原始证书
这是我的证书清单:
Original certificates
This is a list of my certificates:
- ca.pem
- client-key.pem
- client-cert.pem
- server-key.pem
- server-cert.pem
- ca.pem
- client-key.pem
- client-cert.pem
- server-key.pem
- server-cert.pem
以下代码工作正常,只是为了演示我可以用Java连接MySQL服务器:
The following code works fine, just to demonstrate I can connect to the MySQL server in Java:
properties = new Properties();
properties.put("user", "myuser");
properties.put("password", "mypassword");
connection = DriverManager.getConnection("jdbc:mysql://www.mysite.com:3306/mydatabase", properties);
在命令行上使用MySQL时,SSL上的连接工作
我使用以下命令启动MySQL服务器:
Connection over SSL works when using MySQL on command line
I start the MySQL server with the command:
mysqld --ssl-ca=ca.pem --ssl-cert=server-cert.pem --ssl-key=server-key.pem
然后我使用MySQL命令连接到它:
I then connect to it using the MySQL command:
mysql --host=www.mysite.com --user=myuser --password=mypassword --database=mydatabase --ssl-ca=ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem
这是有效的,在查询 SHOW STATUS LIKE'Ssll_cipher'
后,我得到了正确的结果:
This works, and after querying SHOW STATUS LIKE 'Ssl_cipher'
I get the correct result:
+---------------+--------------------+
| Variable_name | Value |
+---------------+--------------------+
| Ssl_cipher | DHE-RSA-AES256-SHA |
+---------------+--------------------+
推荐答案
如果您遇到此问题,请至少确保将您的CA证书导入到JRE的密钥库!
If you are having this problem, at least make sure that your CA certificate is imported into the keystore of the JRE!
keytool -import -noprompt -trustcacerts -alias< uniqueCAAlias> -file ca.pem -keystore< pathtocacerts>
keytool -import -noprompt -trustcacerts -alias <uniqueCAAlias> -file ca.pem -keystore <pathtocacerts>
(您可能需要将ca.pem文件的名称更改为您自己的CA证书文件名)
(You may have to change the name of the "ca.pem" file to your own CA certificate filename)
请记住保持< uniqueCAAlias>独特的东西。
另外,更改< pathtocacerts>这样它就匹配了JVM的路径。请注意,这很可能是最高版本的JRE,但它也可能是SDK的JRE或安装的不同JRE(如C:\Program Files \Java \ _jre1.8.0_25 \lib \\ security\cacerts)
Remember to keep the <uniqueCAAlias> something unique. Also, change the <pathtocacerts> so that it matches the path of your JVM. Note that this is most likely the highest version JRE, but it may also be the JRE of an SDK or a different JRE that is installed (like "C:\Program Files\Java\jre1.8.0_25\lib\security\cacerts")
您需要输入JVM信任库的密码:此密码始终为changeit。
You will need to enter a password for the JVM truststore: this password is always 'changeit'.
我不能保证这会解决这个问题,但这是我在做这个工作时迈出的重要一步,最后我得到了它。
I can't guarantee this will solve the problem, but it was a major step I took while working on this, and I got it working in the end.
这篇关于使用Java与MySQL服务器建立SSL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!