11111111111编写类并继承PropertyPlaceholderConfigurer.java

package com.xx.encryptDecrypt;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.xx.core.encrypt.EncryptUtil;

/**
* <br>
* Title:PropertyPlaceholderConfigurerExt <br>
* Description:PropertyPlaceholderConfigurer扩展
*/
public class PropertyPlaceholderConfigurerExt extends PropertyPlaceholderConfigurer {
private static Map<String, String> propertyMap;
private static Logger log = Logger.getLogger(PropertyPlaceholderConfigurerExt.class);
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
propertyMap = new HashMap<String, String>();
String encryptNames = props.getProperty("encryptNames");
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);

if (encryptNames.contains(keyStr)) {
try {
props.setProperty(keyStr, EncryptUtil.decrypt(value));
propertyMap.put(keyStr, EncryptUtil.decrypt(value));
} catch (Exception e) {
e.printStackTrace();
log.info("解密出错,配置文件的密文可能有误!!!!!!!!!1");
}
} else {
propertyMap.put(keyStr, value);
}
}
System.out.println(propertyMap.toString());

super.processProperties(beanFactoryToProcess, props);
}

// 自定义一个方法,即根据key拿属性值,方便java代码中取属性值
public static String getProperty(String name) {
return propertyMap.get(name);
}
}

22222222222---spring上下文配置

<bean id="propertyConfigurer" class="com.ytd.encryptDecrypt.PropertyPlaceholderConfigurerExt">
         <property name="location" value="classpath:/conf/app.properties"/>
</bean>

3333333333---app.properties文件

账号密码改为密文

jdbc.username=bcc5b4b5174967b7
jdbc.password=bcc5b4b5174967b7

并添加键值对,作为解密标识

encryptNames=jdbc.username,jdbc.password

4444444444加密工具EncryptUtil.java不在贴出

spring--给配置文件.properties加密的更多相关文章

  1. Spring Boot 配置文件密码加密两种方案

    Spring Boot 配置文件密码加密两种方案 jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中.可以快速集成到 Spring Boot 项 ...

  2. spring boot 配置文件properties和YAML详解

    spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...

  3. spring*.xml配置文件明文加密

    spring*.xml配置文件明文加密 说明:客户要求spring*.xml中Oracle/Redis/MongoDB的IP.端口.用户名.密码不能明文存放,接到需求的我,很无奈,但是还是的硬着头皮搞 ...

  4. spring jdbc配置文件进行加密解密

    最近做一个项目,安全上有点要求,就是要对数据库相关的配置进行加密,配置文件如下: #加密前#datasource.type=mysql #datasource.driverClassName=com. ...

  5. Java中如何获取spring中配置文件.properties中属性值

    通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer"   class="co ...

  6. spring boot 配置文件加密数据库用户名/密码

    这篇文章为大家分享spring boot的配置文件properties文件里面使用经过加密的数据库用户名+密码,因为在自己做过的项目中,有这样的需求,尤其是一些大公司,或者说上市公司,是不会把这些敏感 ...

  7. java Spring使用配置文件读取jdbc.properties

    Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...

  8. 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

     spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...

  9. Spring boot 配置文件详解 (properties 和yml )

    从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...

随机推荐

  1. Fluent动网格【9】:区域运动

    本文所述的区域运动并非动网格中的运动域,而是指在多参考系(MRF)或滑移网格中所涉及到的区域的运动. 在滑移网格中指定区域运动时,除了能够指定绝对运动外,还能指定某一区域与其他区域间的相对运动,如图所 ...

  2. 【Spark深入学习 -12】Spark程序设计与企业级应用案例02

    ----本节内容------- 1.遗留问题答疑 1.1 典型问题解答 1.2 知识点回顾 2.Spark编程基础 2.1 Spark开发四部曲 2.2 RDD典型实例 2.3 非RDD典型实例 3. ...

  3. C# 参数签名字符串按 ASCII码排序,注意其中的坑

    参数签名中通常是按键值对中键名称的ASCII按从小到大的顺序排序后进行hash为签名字符串.不要直接使用 SortedDictionary<string, string> 有坑的,他是按数 ...

  4. Safari 3D transform变换z-index层级渲染异常的研究

    by zhangxinxu from http://www.zhangxinxu.com/wordpress/?p=5569 一.Safari是新时代的IE6 在2年前介绍currentColor变量 ...

  5. java maven 编译文件时 有些类型文件 不存在

    在pom.xml中添加如下: <build> <resources> <resource> <directory>src/main/resource&l ...

  6. C语言 sscanf用法详解

    /* sscanf用法详解 */ #include <stdio.h> /* sscanf头文件 */ #include <stdlib.h> #include <str ...

  7. git 中merge的用法

    git merge –no-ff 可以保存你之前的分支历史.能够更好的查看 merge历史,以及branch 状态. git merge 则不会显示 feature,只保留单条分支记录.

  8. Java8学习笔记(十一)--并发与非并发流下reduce比较

    BinaryOperator<Integer> operator = (l, r) -> l + r; BiFunction<Integer, Integer, Integer ...

  9. iOS之HTTP和HTTPS的基本知识和应用

    HTTPS的基本使用 1.https简单说明HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道 ...

  10. linux下的抓包工具tcpdump

    1.由netstat查看网络情况,引出的TCP建立连接.终止连接过程,以及TCP状态分析: 2.Soap=XML+HTTP引出的HTTP协议分析: 3.Soap(Simple Object Acces ...