使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
来源:互联网 发布:爱淘宝抽红包在哪里 编辑:程序博客网 时间:2024/05/16 01:30
一、背景
处于安全考虑需要对.properties中的数据库用户名与密码等敏感数据进行加密。项目中使用了Spring3框架统一加载属性文件,所以最好可以干扰这个加载过程来实现对.properties文件中的部分属性进行加密。
属性文件中的属性最初始时敏感属性值可以为明文,程序第一次执行后自动加密明文为密文。
二、问题分析
- 扩展PropertyPlaceholderConfigurer最好的方式就是编写一个继承该类的子类。
- 外部设置locations时,记录全部locations信息,为加密文件保留属性文件列表。重写setLocations与setLocation方法(在父类中locations私有)
- 寻找一个读取属性文件属性的环节,检测敏感属性加密情况。对有已有加密特征的敏感属性进行解密。重写convertProperty方法来实现。
- 属性文件第一次加载完毕后,立即对属性文件中的明文信息进行加密。重写postProcessBeanFactory方式来实现。
三、代码实现
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!-- 加密 --><property name="locations"><list><value>classpath:db.properties</value></list></property><!-- 声明需要加密的属性 --><property name="encryptedProps"><set><value>db.jdbc.username</value> <value>db.jdbc.password</value> <value>db.jdbc.url</value></set></property></bean>
package com.sgfm.test;import com.sgfm.base.des.DESEncryptUtil;public class Test {private static final String SEC_KEY = "@^_^123aBcZ*"; // 主密钥private static final String ENCRYPTED_PREFIX = "{";// 默认加密前缀private static final String ENCRYPTED_SUFFIX = "}";// 默认加密后缀public static void main(String[] args) throws Exception {DESEncryptUtil dec = new DESEncryptUtil();String encryptStr = dec.encrypt("tcp://192.168.10.242:61616", SEC_KEY);// 加密String decryptStr = dec.decrypt("1B7328C79D9D5BC37E0636947EFEECD6E1459B4260497E31000754134655A350", SEC_KEY);// 解密System.out.println("加密内容:" + encryptStr);System.out.println("解密内容:" + decryptStr);}}原属性文件
db.jdbc.driver=com.mysql.jdbc.Driverdb.jdbc.url=jdbc:mysql://localhost:3306/noah?useUnicode=true&characterEncoding=utf8db.jdbc.username=noahdb.jdbc.password=noah
加密后的文件
db.jdbc.driver=com.mysql.jdbc.Driverdb.jdbc.url=Encrypted:{e5ShuhQjzDZrkqoVdaO6XNQrTqCPIWv8i_VR4zaK28BrmWS_ocagv3weYNdr0WwI}db.jdbc.username=Encrypted:{z5aneQi_h4mk4LEqhjZU-A}db.jdbc.password=Encrypted:{v09a0SrOGbw-_DxZKieu5w}
0 0
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- 使用PropertyPlaceholderConfigurer读取.properties文件(2)-处理加密属性
- spring中的PropertyPlaceholderConfigurer访问properties文件配置
- PropertyPlaceholderConfigurer读Properties文件 对部分属性加密
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- Spring PropertyPlaceholderConfigurer解析Java Properties属性文件值
- spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- 使用PropertyPlaceholderConfigurer读取.properties文件(3)-公共类获取属性
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- 扩展PropertyPlaceholderConfigurer对prop文件中的属性加密
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- Spring属性占位符PropertyPlaceholderConfigurer的使用 ${}
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- Linux IO模式及 select、poll、epoll详解
- IntelliJ IDEA 2017快捷键使用
- 浅析人工智能在视频监控市场的应用
- ios 常用预编译指令
- C#中internal与protected,private等修饰的联系与区别
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- Linux下安装PHP的MongoDB扩展
- 原型链小Demo
- GPUImage详细解析(二)
- Java8 List stream经常用到的方法集合!
- Tinker集成步骤和集成中所需要的问题
- android中实现暗码启动apk【一】
- 数据结构实验之图论四:迷宫探索
- ubuntu安装oracle客户端