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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1273403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:54:03  来源:igfitidea点击:

How can I configure Hibernate to use SSL to talk to the DB server?

javahibernateconfiguration

提问by BZ.

I have an existing java webapp that uses Hibernate for it's persistence. I've been told that I have to have to talk to the DB encrypted - so my first thought is to set it up to do the communication via SSL - and went through figured out how to set up Oracle to listen for JDBC over SSL -

我有一个现有的 java webapp,它使用 Hibernate 来实现它的持久性。有人告诉我,我必须与加密的 DB 交谈 - 所以我的第一个想法是将它设置为通过 SSL 进行通信 - 并研究了如何设置 Oracle 以通过 SSL 侦听 JDBC -

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

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

And wrote a quick test class to verify that it was setup and working (connecting via standard JDBC). That left me with the issue of configuring Hibernate - unfortunately I don't see how hibernate supports it?

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

采纳答案by andri

Hibernate works with standard JDBC data sources, so there is no need for Hibernate-specific configuration.

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

Here's an quick example that should work when configuring Hibernate with Spring:

这是一个在使用 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>

回答by Ramson Tutte

Try this:

试试这个:

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

related links

相关链接

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

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://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/

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

回答by Jayanta Debnath

Please add following property in Hibernate configuration file to enable SSL :

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

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

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

回答by Gren

Should be handled by the driver but you may have to do some configuration. Oracle Docs

应该由驱动程序处理,但您可能需要进行一些配置。 甲骨文文档