如何配置 Hibernate 以使用 SSL 与数据库服务器通信?

Posted

技术标签:

【中文标题】如何配置 Hibernate 以使用 SSL 与数据库服务器通信?【英文标题】:How can I configure Hibernate to use SSL to talk to the DB server? 【发布时间】:2010-11-19 09:26:03 【问题描述】:

我有一个现有的 java webapp,它使用 Hibernate 来实现它的持久性。有人告诉我,我必须与加密的数据库对话——所以我的第一个想法是设置它以通过 SSL 进行通信——并弄清楚如何设置 Oracle 以通过 SSL 侦听 JDBC——

http://www.oracle.com/technology/tech/java/sqlj_jdbc/pdf/wp-oracle-jdbc_thin_ssl_2007.pdf

并编写了一个快速测试类来验证它是否已设置和工作(通过标准 JDBC 连接)。这给我留下了配置 Hibernate 的问题 - 不幸的是,我没有看到 hibernate 是如何支持它的?

【问题讨论】:

该链接已失效,我相信这是更新版本:oracle.com/technetwork/topics/… 【参考方案1】:

Hibernate 使用标准 JDBC 数据源,因此不需要特定于 Hibernate 的配置。

下面是一个在使用 Spring 配置 Hibernate 时应该工作的简单示例:

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
    <property name="URL"><value><!-- JDBC URL that specifies SSL connection --></value></property>
    <!-- other relevant properties, like user and password -->
    <property name="connectionProperties>
        <value>
            oracle.net.ssl_cipher_suites: (ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)
            oracle.net.ssl_client_authentication: false
            oracle.net.ssl_version: 3.0
            oracle.net.encryption_client: REJECTED 
            oracle.net.crypto_checksum_client: REJECTED
        </value>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- classes etc -->
</bean>

【讨论】:

但是我必须在驱动程序上设置一些属性 - 使用 JDBC 我可以这样做: prop .setProperty("oracle.net.ssl_cipher_suites", "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)"); prop.setProperty("oracle.net.ssl_client_authentication", "false"); prop.setProperty("oracle.net.ssl_version", "3.0"); prop.setProperty("oracle.net.encryption_client", "REJECTED"); prop.setProperty("oracle.net.crypto_checksum_client", "REJECTED");但是如何通过休眠获取与驱动程序关联的属性? 这取决于您如何配置 Hibernate。稍后我将添加一个您可以与 Spring 一起使用的示例。 如果您不使用 Spring,请参阅 Hibernate 文档的表 3.4:docs.jboss.org/hibernate/stable/core/reference/en/html/… - 虽然我没有测试过,但您似乎可以通过添加 hibernate 将 JDBC 属性传递给 Hibernate。连接。* 属性。【参考方案2】:

试试这个:

    <property name="hibernate.dialect">org.hibernate.dialect.mysqlInnoDBDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://blablaba:8443/dbname?useSSL=true</property>
    <property name="hibernate.connection.verifyServerCertificate">false</property>
    <property name="hibernate.connection.requireSSL">true</property>
    <property name="hibernate.connection.autoReconnect">true</property>
    <property name="hibernate.connection.username">bablablab</property>
    <property name="hibernate.connection.password">clclclclc</property>

相关链接

http://www.razorsql.com/articles/mysql_ssl_jdbc.html

http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-using-ssl.html

http://www.javabeat.net/qna/164-hibernate-jdbc-and-connection-properties/

【讨论】:

我试过你的配置,但没有任何效果。【参考方案3】:

请在 Hibernate 配置文件中添加以下属性以启用 SSL:

<property name="hibernate.connection.verifyServerCertificate">false</property> <property name="hibernate.connection.useSSL">true</property>

【讨论】:

这个答案中是否有其他答案尚未提及的新内容? 使用的其他答案:&lt;property name="hibernate.connection.requireSSL"&gt;true&lt;/property&gt; 但对我而言,它没有用。我必须使用以下方法才能使其工作:&lt;property name="hibernate.connection.useSSL"&gt;true&lt;/property&gt;【参考方案4】:

应该由驱动程序处理,但您可能需要进行一些配置。 Oracle Docs

【讨论】:

以上是关于如何配置 Hibernate 以使用 SSL 与数据库服务器通信?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Hibernate 4 中配置日志记录以使用 SLF4J

如何在 Hibernate 4 中配置日志记录以使用 SLF4J

如何使用 maven 配置 hibernate-tools 以生成 hibernate.cfg.xml、*.hbm.xml、POJO 和 DAO

如何配置 Hibernate 5 以强制 Mysql 使用 Innodb 引擎创建表?

如何配置 Spring Boot 应用程序以在 MySQL 上使用 SSL/TLS?

如何配置后端服务器以使用由 aws gateway api 生成的客户端 SSL 证书?