我可以隐藏来自hibernate cfg文件的数据库连接详细信息吗?

时间:2017-05-15 07:06:47

标签: database hibernate

只需检查,如果有隐藏数据库连接详细信息的方式,如hibernate配置文件中的用户名或密码:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
    <property name="username" value="system"></property>
    <property name="password" value="tiger"></property>
</bean>

1 个答案:

答案 0 :(得分:1)

您有2个选项

1)使用JNDI

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/DatabaseName" expected-type="javax.sql.DataSource" />

2)使用属性占位符

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:database.properties</value>
        <!-- this example use the system parameter configlocation, add -Dconfiglocation -->
        <value>file:${configlocation}/database.properties</value>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
    <property name="username" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

密码可以在文件database.properties中,该文件只在正在运行的应用程序的类路径中,并且对所有人都不可见。