Properties配置文件数据加密
来源:互联网 发布:大数据与经济发展 编辑:程序博客网 时间:2024/06/08 04:05
在一些情况下,需要对properties中的配置数据进行加密。比如Mysql数据库的密码,不应该以明文的形式直接保存在properties中,应该以密文的形式保存.
PropertiesUtil
用于读取properties配置文件的数据,并且将加密的密文进行解密.
/** * Parsing The Perperties Configuration File */public final class PropertiesUtil extends PropertyPlaceholderConfigurer { private static final byte[] KEY = { -81, 0, 105, 7, -32, 26, -49, 88 }; private static Map<String, String> ctxPropertiesMap; // 用于存放perperties中的数据 private List<String> decryptProperties; // 用于存放需要解密的key @Override protected void loadProperties(Properties props) throws IOException { super.loadProperties(props); ctxPropertiesMap = new HashMap<String, String>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); if (decryptProperties != null && decryptProperties.contains(keyStr)) { value = SecurityUtil.decryptDes(value); // 解密 props.setProperty(keyStr, value); // 设置解密后的明文数据 } ctxPropertiesMap.put(keyStr, value); } } /** * @param decryptPropertiesMap * the decryptPropertiesMap to set */ public void setDecryptProperties(List<String> decryptProperties) { this.decryptProperties = decryptProperties; } /** * Get a value based on key , if key does not exist , null is returned * * @param key * @return */ public static String getString(String key) { try { return ctxPropertiesMap.get(key); } catch (MissingResourceException e) { return null; } } /** * 根据key获取值 * * @param key * @return */ public static int getInt(String key) { return Integer.parseInt(ctxPropertiesMap.get(key)); } /** * 根据key获取值 * * @param key * @param defaultValue * @return */ public static int getInt(String key, int defaultValue) { String value = ctxPropertiesMap.get(key); if (StringUtils.isBlank(value)) { return defaultValue; } return Integer.parseInt(value); } /** * 根据key获取值 * * @param key * @param defaultValue * @return */ public static boolean getBoolean(String key, boolean defaultValue) { String value = ctxPropertiesMap.get(key); if (StringUtils.isBlank(value)) { return defaultValue; } return new Boolean(value); } public static void main(String[] args) { String encrypt = SecurityUtil.encryptDes("12345", KEY); System.out.println(encrypt); System.out.println(SecurityUtil.decryptDes(encrypt, KEY)); }}
jdbc.properties
db.driver=com.mysql.jdbc.Driverdb.reader.url=jdbc:mysql://127.0.0.1:3306/mydatabasedb.reader.username=rootdb.reader.password=siUZ0QNpCcq=
spring中的配置文件
<!-- 引入属性配置文件 --> <bean class="com.spring.util.PropertiesUtil"> <!--加载properties配置文件 --> <property name="locations"> <list> <value>classpath:config/jdbc.properties</value> </list> </property> <!--需要进行解密的数据对 --> <property name="decryptProperties"> <array> <!-- 需要解密的配置 --> <value>db.reader.password</value> </array> </property> </bean> <!-- 引入属性配置文件 --> <bean id="connectionConfig" class="com.spring.bean.ConnectionConfig"> <property name="password" value="${db.reader.password}" /> </bean>
输出的密码,是解密后的明文
测试代码
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-*.xml"); System.out.println(applicationContext.getBean("connectionConfig"));}
测试输出结果
ConnectionConfig [password=12345]
测试代码下载
代码下载
阅读全文
1 0
- Properties配置文件数据加密
- Java jasypt对.properties配置文件密码加密
- Java实现properties配置文件加密解密
- properties配置文件中动态存取数据
- 读取***.properties配置文件里数据的方法
- Properties工具类获取配置文件数据
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- Spring加载Properties配置文件的加密解密处理
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- Properties配置文件
- Properties配置文件
- Properties配置文件
- properties配置文件
- Properties配置文件
- properties配置文件
- 采用DES加密方式对properties配置文件敏感信息加密处理
- 用ServletContext获取properties配置文件中的一些数据
- request.getRequestURI()的使用
- maven
- JSP内置对象的使用:request,cookie,session
- Android启动流程(一)
- 看雪ctf2017第一题详解
- Properties配置文件数据加密
- Java抽象类
- shell-grammar
- Android Studio 使用教程(二十一)之Android Studio如何快速生成get和set
- map根据其value值进行排序
- 关于SpringDataJPA的一些小知识
- Hdu A+B for Input-Output Practice (VII)
- Java指定保留小数位数的方法
- 解决Python字符串显示u'...'的问题