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:6D7950617373776F72644.测试类
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
- SPRING使用占位符读取字段加密的properties文件
- spring xml读取Properties文件中的加密字段
- Spring读取properties文件加密解密
- 使用spring读取properties文件
- spring读取properties属性文件,并替换占位符中的值.
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- spring+mybatis整合时占位符无法读取jdbc.properties的问题
- .properties文件读取及占位符${...}替换源码解析
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- Spring中属性文件properties的读取与使用
- RedHat9 安装JAVA JDK6
- 性能测试中用LambdaProbe监控Tomcat
- 类似京东的商品筛选功能的实现,电子商城的产品筛选功能是怎么实现的?
- 安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发的每个领域
- 产品管理,产品策划,产品设计
- SPRING使用占位符读取字段加密的properties文件
- VMWare文件复制和移动的区别
- dllregisterserver 调用失败, 错误代码为0x80004005 的解决办法
- 使用sqlbulkcopy批量插入
- 亮度值与RGB的转换
- 增强视觉 | 计算机视觉 增强现实 开源势力
- android: what is the difference between focused, enabled, pressed, and selected states?
- test
- Android中的线程机制(Handler Looper)(二)