zoukankan      html  css  js  c++  java
  • SPRING使用占位符读取字段加密的properties文件

    最近遇见几人问这个问题,自己以后也肯能会遇见,主要是对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 
  • 相关阅读:
    Pimlico:个人信息治理套件
    GChemPaint-绘制化学分子布局
    gLabels:名片方案软件
    设置Tomcat在Linux利用体系中自启动办法
    Linux应用系统下Xmanager器材登录设置
    KGmailNotifier-Gmail 邮件关照轨范
    Xfce 4.4.1 发布
    Mozilla Thunderbird 2.0.0.0 正式版颁布
    Gaim 已更名为 Pidgin
    Wine 0.9.35
  • 原文地址:https://www.cnblogs.com/huapox/p/3516120.html
Copyright © 2011-2022 走看看