spring使用外部属性文件--加密

这篇文章和下面两篇文章有关联,请先阅读下面两篇文章。

http://xieyan30.iteye.com/admin/blogs/1831311

http://xieyan30.iteye.com/admin/blogs/1831034

说到spring应用外部属性文件,有时我们需要对外部属性文件中特殊的信息进行加密处理,但是spring的PropertyPlaceholderConfigurer类没有提供对密文版的属性文件支持,所以我们需要自己去扩展这个类。

示例:

1,对jdbc.properties中的userName,password进行加密处理。同时演示了properties文件引用自身的属性(url引用了dbName)。

driverClassName=com.mysql.jdbc.Driver
dbName=D8yx9QXmK5E=
url=jdbc:mysql://localhost:3306/${dbName}
userName=nskJzTSqMLk=
password=iFIkcE3fz5I=

 2,扩展PropertyPlaceholderConfigurer

package spring3.pripertyFile;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {

	private static final String[] encryptPropNames = { "userName", "password", "dbName" };

	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
		if (isEncryptProp(propertyName)) {
			return DESUtils.getDecryptString(propertyValue);
		} else {
			return propertyValue;
		}
	}

	private boolean isEncryptProp(String propertyName) {
		for (String encryptPropName : encryptPropNames) {
			if (encryptPropName.equals(propertyName)) {
				return true;
			}
		}
		return false;
	}
}

 3,配置我们的扩展类

如果使用自定义的属性文件扩展类,<context:property-placeholder>就无法使用了,要使用传统的配置方案

由于本人机器上没有真实的DB,所以做个测试类进行演示,原理一样。请勿见怪。

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <!-- 引入外部属性文件 -->
    <bean class="spring3.pripertyFile.EncryptPropertyPlaceholderConfigurer"
          p:location="classpath:spring3/pripertyFile/jdbc.properties"
          p:fileEncoding="utf-8"/>

<!--
    <bean id="dataSource" 
          class="org.apache.commons.dbcp.BasicDataSource" 
          destroy-method="close" 
          p:driverClassName="${driverClassName}" 
          p:url="${url}" 
          p:userName="${userName}" 
          p:password="${password}"/>
-->

    <bean id="dataSource" 
          class="spring3.pripertyFile.DataSourceTest" 
          p:driverClassName="${driverClassName}" 
          p:url="${url}" 
          p:userName="${userName}" 
          p:password="${password}"/>
</beans>

 4,测试是否取得正确的属性值

package spring3.pripertyFile;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring3/pripertyFile/spring.xml");
		DataSourceTest dataSource = (DataSourceTest) context
				.getBean("dataSource");
		System.out.println(dataSource.getDriverClassName());
		System.out.println(dataSource.getUrl());
		System.out.println(dataSource.getUserName());
		System.out.println(dataSource.getPassword());
	}
}

 结果:

com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/testdb
root
1234

猜你喜欢

转载自xieyan30.iteye.com/blog/1831351