繁体   English   中英

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

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

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

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

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

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>

试试这个:

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

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

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

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

我有 jdbcURL jdbc:postgresql://jdbcurl?sslmode=require&sslrootcert=location_to_cert1&sslcert=location_to_cert2&sslkey=location_to_cert3

我所要做的就是将所有&替换为&amp; .

我的新 jdbcURL 看起来像jdbc:postgresql://jdbcurl?sslmode=require&amp;sslrootcert=location_to_cert1&amp;sslcert=location_to_cert2&amp;sslkey=location_to_cert3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM