0

下面是我在 hibernate-config.xml 文件中的 bean

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

${} 中的所有值都来自属性文件并自动设置。但是我在属性文件中的密码是加密格式的,所以我想在解密后设置它。这个怎么做?

4

3 回答 3

0

定义一个 Spring FactoryBean,它将解码“密码”值并返回它。Factory Beans 可用于回答/计算值,以及创建对象。

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/FactoryBean.html

在您的 Spring 配置中引用 bean,然后将为您提供已回答的 value

如果您需要 Properties 对象中的值并且无法在其中获取它,您可以创建一个 Properties 工厂,它将解码的值与配置的属性结合起来。

于 2013-05-16T12:20:25.533 回答
0

用你的类子类化 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>
于 2013-05-16T16:10:35.523 回答
0

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

<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

于 2013-05-16T10:19:19.447 回答