【问题标题】:Configure spring to connect to mysql over ssl配置spring通过ssl连接mysql
【发布时间】:2012-12-25 06:21:34
【问题描述】:

我正在从我的 Java 应用程序通过 SSL 连接到 MySQL。我已将 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);

我想使用带有 C3p0 的 spring 通过 SSL 连接到 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 =true
使用SSL=true
要求SSL=true"

也可以在 spring 配置文件中设置 keyStore 和 trustStore 值。

【问题讨论】:

    标签: spring ssl jdbc c3p0 spring-jdbc


    【解决方案1】:

    jdbc.properties 中jdbc.url 的值必须为

    jdbc:mysql://127.0.0.1:3306/MySampleDb?verifyServerCertificate=true&useSSL=true&requireSSL=true

    这些参数必须直接添加到 MySQL 的 URL。 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
    

    可以use Spring to set system properties但我永远不会使用它,它太麻烦了。

    【讨论】:

      【解决方案2】:

      不必将keyStoretrustStore 传递给java 程序或设置任何系统属性,因为它可以通过每个连接的连接属性来实现!

      因此,您可以为不同的连接使用不同的证书(如果您在应用服务器中,还可以使用不同的应用程序)。

      原答案:https://stackoverflow.com/a/51879119/173149相关部分:

      jdbc:mysql://example.com:3306/MYDB?verifyServerCertificate=true&useSSL=true&requireSSL=true&clientCertificateKeyStoreUrl=file:cert/keystore.jks&clientCertificateKeyStorePassword=123456&trustCertificateKeyStoreUrl=file:cert/truststore.jks&trustCertificateKeyStorePassword=123456

      记录在案:

      【讨论】:

        【解决方案3】:

        您可以使用基于 Java 的配置来配置 DataSourceuseSSlrequireSSLverifyServerCertificate 属性。 DataSource 类的addDataSourceProperty 方法为您提供了能力,如下面的代码 sn-p 所示(您可以将 HikariDataSource 替换为 C3p0 实例)

        MySQL Connector/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;
        }
        

        【讨论】:

          猜你喜欢
          • 2018-07-02
          • 2018-07-16
          • 2014-06-17
          • 1970-01-01
          • 2020-11-19
          • 2019-10-15
          • 2018-11-21
          • 1970-01-01
          • 2019-12-04
          相关资源
          最近更新 更多