配置spring通过ssl连接到mysql [英] Configure spring to connect to mysql over ssl
问题描述
我正在从Java应用程序通过SSL连接到MySQL.我已将MYSQL配置为支持SSL和生成的客户端证书.我已将服务器CA证书和客户端证书导入密钥库.这就是我的代码当前的样子
I am connecting to MySQL over SSL from my Java application. I have configured MYSQL to support SSL and generated client certificates. I have imported server CA certificate and client certificate into keystore. This is how my code currently looks like
String url = "jdbc:mysql://127.0.0.1:3306/MySampleDb? verifyServerCertificate =true&useSSL=true&requireSSL=true"
System.setProperty("javax.net.ssl.keyStore","/home/cert/keystore");
System.setProperty("javax.net.ssl.keyStorePassword","password");
System.setProperty("javax.net.ssl.trustStore","/home/cert/truststore");
System.setProperty("javax.net.ssl.trustStorePassword","password");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
我想使用带有C3p0的spring来通过SSL连接到MYSQL.这是我的spring配置文件,它从jdbc.properties中读取参数.
I want to use spring with C3p0 to connect to MYSQL over SSL.This is my spring configuration file which reads parameters from jdbc.properties.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
........
</bean>
如何配置spring来设置属性
verifyServerCertificate = true
useSSL = true
requireSSL = true"
也可以在spring配置文件中设置 keyStore和trustStore 值.
How can I configure spring to set properties
verifyServerCertificate =true
useSSL=true
requireSSL=true"
Also is it possible to set keyStore and trustStore values in spring config file.
推荐答案
jdbc.properties中jdbc.url
的值必须为
The value for jdbc.url
in jdbc.properties has to be
jdbc:mysql://127.0.0.1:3306/MySampleDb?verifyServerCertificate = true& useSSL = true& requireSSL = true
jdbc:mysql://127.0.0.1:3306/MySampleDb?verifyServerCertificate=true&useSSL=true&requireSSL=true
这些参数必须直接添加到MySQL的URL中. keyStore
和trustStore
的参数应在启动时传递给JVM,如下所示:
Those parameters must be added directly to the URL for MySQL. The parameters for keyStore
and trustStore
should be passed to the JVM at start like so:
-Djavax.net.ssl.keyStore=path_to_keystore_file
-Djavax.net.ssl.keyStorePassword=password
-Djavax.net.ssl.trustStore=path_to_truststore_file
-Djavax.net.ssl.trustStorePassword=password
您可以 使用Spring设置系统属性,但我永远不会使用它,太麻烦了.
You can use Spring to set system properties but I'd never use it, it's too cumbersome.
这篇关于配置spring通过ssl连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!