Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件

来源:互联网 发布:淘宝订单号大小怎么看 编辑:程序博客网 时间:2024/06/05 08:14
  当我们在项目中配置数据源时,经常会将其对应的一些属性值写到另外的属性文件中,这样的好处是可以简化项目维护和部署工作,当项目从开发环境迁移到生产环境的时候,运维人员只需要修改数据源对应的属性文件就可以了,无需关注其他的配置文件。如果在属性文件中将数据库的用户名和密码等敏感信息以明文的方式写在文件中,这是非常不安全的,所以我们就需要将属性文件中的部分信息进行加密处理以提高安全性。下面介绍如何运用spring中的PropertyPlaceholderConfigurer类对加密的属性值进行处理。

  假设数据源配置信息放在jdbc.properties文件中:

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=****
PropertyPlaceholderConfigurer类本身对加密的数据不做任何处理的,所以我们需要自定义类继承PropertyPlaceholderConfigurer,并且重写父类中的方法

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Overrideprotected void convertProperties(Properties props) {Enumeration<?> propertyNames = props.propertyNames();while (propertyNames.hasMoreElements()) {String propertyName = (String) propertyNames.nextElement();String propertyValue = props.getProperty(propertyName);String convertedValue = convertProperty(propertyName, propertyValue);if (propertyName.equals("jdbc.password")) {//此处调用解密方法对密文进行解密(本例中省略),本例中为演示方便,直接赋值解密后的值convertedValue = "root";}if (!ObjectUtils.nullSafeEquals(propertyValue, convertedValue)) {props.setProperty(propertyName, convertedValue);}}}
}
spring配置文件applicationContext.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop/spring-tx-2.5.xsd     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。 --><bean id="propertyConfigurer" class="util.MyPropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property><property name="fileEncoding"><value>UTF-8</value></property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><!-- 指定JDBC驱动类 --><property name="driverClassName" value="${jdbc.driverClassName}"></property><!-- 提供连接数据库的URL地址 --><property name="url" value="${jdbc.url}"></property><!-- 提供连接数据库的用户名和密码 --><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><!-- <property name="hbm2ddl.auto" value="update"></property> --></bean><!-- 定义SessionFactory Bean --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 为LocalSessionFactoryBean注入定义好的数据源 --><property name="dataSource"><ref bean="dataSource" /></property><!-- 添加Hibernate配置参数 --><property name="hibernateProperties"><props><!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> --><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">false</prop><!-- <prop key="hbm2ddl.auto">update</prop> --><prop key="current_session_context_class">thread</prop></props></property><!-- 添加对象关系映射文件 --><!-- <property name="mappingResources"><list><value>hbmcfg/User.hbm.xml</value></list></property> --><property name="mappingDirectoryLocations"><list><value>classpath:hbmcfg/</value></list></property></bean><bean id="userDaoImpl" name="userDaoImpl" class="dao.impl.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory"></property></bean></beans>




阅读全文
0 0
原创粉丝点击