2013-05-16 24 views
0

下面是我在休眠-config.xml文件豆休眠与Spring:如何解密数据库密码放在属性文件Hibernate配置

<bean id="myDataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> 
     <property name="url" value="jdbc:mysql://${indy.web.database.host}:${indy.web.database.port}/${indy.web.database.name}" /> 
     <property name="user" value="${indy.web.database.login}" /> 
     <property name="password" value="${indy.web.database.password}" /> 
    </bean> 

所有值在$ {}通过属性文件来了并自动设置。 但我在属性文件中的密码是加密格式,所以我想在解密后设置它。 如何做到这一点?

回答

0

实现此目的的一种简单方法是使用jasypt提供的属性占位符配置器而不是构建属性palceholder configurer。

<bean id="propertyConfigurer" 
    class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer"> 
<constructor-arg ref="configurationEncryptor" /> 
<property name="locations"> 
    <list> 
    <value>/WEB-INF/classes/application.properties</value> 
    </list> 
    </property> 
</bean> 

全部配置在此提供的详细信息页面 - http://www.jasypt.org/spring3.html

+0

我有我自己的加密和解密算法,我必须使用因为它用于我的其他非弹性服务 –

+0

您可以使用jasypt配置加密算法,也可以实现您自己的PropertyPlaceHolderCon (它扩展了Spring的PropertyPlaceHolderConfigurer),类似于EncryptablePropertyPlaceholderConfigurer。 – gkamal

0

Sublass MysqlDataSource与你的类

事情是这样的:

public class DatasourceWithEncPass extends MysqlDataSource{ 
    public void setPassword(String passowrd){ 
    //decrypt the password here before setting it 
    } 
.. 
} 

使用此类上下文:

<bean id="myDataSource" class="com.yourpackage.DatasourceWithEncPass"> 
     <property name="url" value="jdbc:mysql://${indy.web.database.host}:${indy.web.database.port}/${indy.web.database.name}" /> 
     <property name="user" value="${indy.web.database.login}" /> 
     <property name="password" value="${indy.web.database.password}" /> 
    </bean> 
+0

谢谢我做了完全一样的yesteraday夜 –