SPRING使用占位符读取字段加密的properties文件

最近遇见几人问这个问题,自己以后也肯能会遇见,主要是对spring读取的properties加密后的处理
1.继承实现PropertyPlaceholderConfigurer

package com.zhangyz.www.spring;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class MinePropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	private String encrypt = "after-encrypt:";
	private static Logger logger = Logger
			.getLogger(MinePropertyPlaceholderConfigurer.class.getName());

	protected String convertPropertyValue(String originalValue) {
		if (originalValue != null && originalValue.length() > encrypt.length()
				&& originalValue.indexOf(encrypt) >= 0) {
			return new String(hex2Byte(originalValue.substring(encrypt.length())));
		} else {
			return originalValue;
		}
	}

	static final String HEXES = "0123456789ABCDEF";

	public static String byte2Hex(byte[] bs) {
		StringBuffer hex = new StringBuffer(2 * bs.length);
		for (int i = 0; i < bs.length; i++) {
			hex.append(HEXES.charAt((bs[i] & 0xF0) >> 4)).append(
					HEXES.charAt((bs[i] & 0x0F)));
		}
		return hex.toString();
	}

	public static byte[] hex2Byte(String hex) {
		if (hex.length() % 2 != 0) {
			throw new RuntimeException("hex is error!");
		}
		byte[] bs = new byte[hex.length() / 2];
		for (int i = 0; i < bs.length; i++) {
			bs[i] = Byte.parseByte(hex.substring(i * 2, i * 2 +2), 16);
		}
		return bs;
	}

	public static void main(String[] args) {
		String password = "myPassword";
		System.out.println(byte2Hex(password.getBytes()));
		System.out.println(new String(hex2Byte(byte2Hex(password.getBytes()))));
	}
}
2.application.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean name="" class="com.zhangyz.www.spring.MinePropertyPlaceholderConfigurer">
		<property name="location">
            <value>classpath:com/zhangyz/www/spring/jdbc.properties</value>
        </property>
	</bean>
	<bean name="test" class="com.zhangyz.www.spring.Test">
		<property name="userName" value="${test.userName}">
		</property>
		<property name="password" value="${test.password}">
		</property>
	</bean>
</beans>
3.properties文件
test.userName=myUserName
test.password=after-encrypt:6D7950617373776F7264
4.测试类
package com.zhangyz.www.spring;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	private String userName;
	private String password;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private static Logger logger = Logger.getLogger(Test.class.getName());

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"/com/zhangyz/www/spring/application.xml");
		Test test=(Test) applicationContext.getBean("test");
		logger.error(test.getUserName());
		logger.error(test.getPassword());
	}
}

5.输出结果

2011-11-30 10:06:22,921 ERROR [main - (Test.java:36) - com.zhangyz.www.spring.Test] myUserName
2011-11-30 10:06:22,921 ERROR [main - (Test.java:37) - com.zhangyz.www.spring.Test] myPassword

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游一游走一走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值