配置Spring连接到MySQL通过SSL(Configure spring to connect

2019-07-18 02:45发布

我连接到MySQL通过SSL从我的Java应用程序。 我已经配置MySQL来支持SSL和生成的客户端证书。 我已经导入服务器CA证书和客户端证书到密钥库。 这是我的代码目前看起来像

    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);

我想对SSL.This使用与C3P0弹簧连接到MySQL是我的spring配置文件,该文件读取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 =真
useSSL =真
requireSSL =真”
也可以在Spring配置文件中设置密钥库和信任值。

Answer 1:

对于价值jdbc.url在jdbc.properties必须是

JDBC:MySQL的://127.0.0.1:3306 / MySampleDb verifyServerCertificate =真useSSL =真requireSSL =真

这些参数必须直接添加到URL为MySQL。 对于参数keyStoretrustStore应该传递给JVM在开始就像这样:

-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来设置系统属性 ,但我从来没有使用它,它是太麻烦了。



Answer 2:

这是没有必要通过keyStoretrustStorejava程序或设置任何系统性能,因为它可以通过连接属性每个连接可以实现!

所以,(如果你是在应用服务器和应用程序),可以使用不同的连接不同的认证。

原来的答复: https://stackoverflow.com/a/51879119/173149有关部分:

JDBC:MySQL的://示例.com:3306 / MYDB verifyServerCertificate =真useSSL =真requireSSL =真clientCertificateKeyStoreUrl =文件:CERT / keystore.jks&clientCertificateKeyStorePassword = 123456&trustCertificateKeyStoreUrl =文件:CERT / truststore.jks&trustCertificateKeyStorePassword = 123456

据记载:

  • https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
  • https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-using-ssl.html


Answer 3:

您可以配置的利用UseSSL,requireSSLverifyServerCertificate性能DataSource ,通过使用基于Java的配置。 所述addDataSourceProperty所述的方法DataSource类给你的能力,如示于下代码段(可以用C3P0实例来替换HikariDataSource)

MySQL连接/ J展示用于密钥存储(例如配置属性trustCertificateKeyStoreUrl ),所以我假定addDataSourceProperty可用于这些特性太。

我不知道如果XML配置模式提供对应标签addDataSourceProperty

public DataSource createPslDataSource(final MyDataSourceProperties myDataSourceProperties) {

    HikariDataSource dataSource = new HikariDataSource();

    dataSource.addDataSourceProperty("useSSL", true);
    dataSource.addDataSourceProperty("requireSSL", true);
    dataSource.addDataSourceProperty("verifyServerCertificate", true);

    dataSource.setJdbcUrl(myDataSourceProperties.getJdbcUrl());
    dataSource.setUsername(myDataSourceProperties.getUsername());
    dataSource.setPassword(myDataSourceProperties.getPassword());

    return dataSource;
}


文章来源: Configure spring to connect to mysql over ssl