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

来源:互联网 发布:linux怎么安装ttc字体 编辑:程序博客网 时间:2024/05/29 00:32

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

package com.zhangyz.www.spring;import org.apache.log4j.Logger;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class MinePropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {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=myUserNametest.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] myUserName2011-11-30 10:06:22,921 ERROR [main - (Test.java:37) - com.zhangyz.www.spring.Test] myPassword

原创粉丝点击