博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPRING使用占位符读取字段加密的properties文件
阅读量:7176 次
发布时间:2019-06-29

本文共 3454 字,大约阅读时间需要 11 分钟。

最近遇见几人问这个问题,自己以后也肯能会遇见,主要是对spring读取的properties加密后的处理
1.继承实现PropertyPlaceholderConfigurer
[java] view plaincopyprint?
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文件
[html] view plaincopyprint?
<?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文件
[html] view plaincopyprint?
test.userName=myUserName  
test.password=after-encrypt:6D7950617373776F7264  
4.测试类
[java] view plaincopyprint?
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.输出结果
[html] view plaincopyprint?
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 

转载于:https://www.cnblogs.com/huapox/archive/2013/05/01/3516120.html

你可能感兴趣的文章
NanoJIT
查看>>
一个最简单GAL游戏资源文件黑盒分析(二)
查看>>
SQL Server 2005允许远程连接的配置说明
查看>>
HQL 语句
查看>>
全神贯注!聚精会神!一心一意!
查看>>
IBATIS事务处理 - - 博客频道 - CSDN.NET
查看>>
编程算法基础-数字数码管-隐藏password
查看>>
C++ - new与malloc的差别
查看>>
使用Html和ashx文件实现其简单的注册页面
查看>>
ZeroMQ接口函数之 :zmq_inproc – ØMQ 本地进程内(线程间)传输方式
查看>>
C#语言基础原理及优缺点
查看>>
AIX系统开启ftp服务
查看>>
linux 上拷贝文件到windows 上 文件出现锁的文件
查看>>
Xamarin iOS教程之编辑界面编写代码
查看>>
Construct Binary Tree from Preorder and Inorder Traversal
查看>>
写得好 git 提交信息
查看>>
Linux下获取线程TID的方法
查看>>
Redis和Memcache的区别分析(转)
查看>>
网络请求 http get post 一
查看>>
《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)
查看>>