Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中 
一些动态设定的值(value),在XML中替换为占位该键($key$)的值, 
.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。

xml中的配置文件

<bean id="propertyConfigurer"
class="com.datasure.util.filter.EncryptablePropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:jdbc.properties</value></list>
</property>
</bean>
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static final String key = "helloworld"; protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
Des des = new Des();
String username = props.getProperty("jdbc.username");
if (username != null) {
props.setProperty("jdbc.username", des.Decrypt(username, des.hex2byte(key)));
} String password = props.getProperty("jdbc.password");
if (password != null) {
props.setProperty("jdbc.password", des.Decrypt(password, des.hex2byte(key)));
} String url = props.getProperty("jdbc.url");
if (url != null) {
props.setProperty("jdbc.url", des.Decrypt(url, des.hex2byte(key)));
} String driverClassName = props.getProperty("jdbc.driverClassName");
if(driverClassName != null){
props.setProperty("jdbc.driverClassName", des.Decrypt(driverClassName, des.hex2byte(key)));
} super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}

通过如上配置就可以对配置文件进行解密操作

解密Spring加载的Properties文件的更多相关文章

  1. 【转载】加密Spring加载的Properties文件

    目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...

  2. SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...

  3. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  4. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  5. Java实现动态加载读取properties文件

    问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties ...

  6. spring加载hibernate映射文件的几种方式。转自:http://blog.csdn.net/huiwenjie168/article/details/7013618

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionF ...

  7. java web项目启动时自动加载自定义properties文件

    首先创建一个类 public class ContextInitListener implements ServletContextListener 使得该类成为一个监听器.用于监听整个容器生命周期的 ...

  8. spring加载hibernate映射文件的几种方式 (转)

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个 Bean实例中进行的,若配置的映射文件较少时,可以用session ...

  9. spring加载hibernate映射文件的几种方式

    1. 在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessi ...

随机推荐

  1. HAProxy advanced Redis health check---ref

    http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...

  2. 1.Vue.js的常用指令

      Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得 ...

  3. C#事件(event)解析委托

    namespace Vczx.ProCSharp.Event { /// <summary> /// 类EatEventArgs 必须继承自类EventArgs,用来引发事件时封装数据 / ...

  4. Unity3D第二课之通过键盘、鼠标移动物体

    public class xuanzhuan : MonoBehaviour { //平移速度变量 public float MoveSpeed;// Use this for initializat ...

  5. easyui-textbox 绑定事件

    $('#Id').textbox({ inputEvents: $.extend({},$.fn.textbox.defaults.inputEvents,{ keyup:function(event ...

  6. 位运算(4)——Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  7. Highcharts - Bar Chart & Column Chart

    1. 条形图(Bar Chart)需要的数据格式类型如下: ["Luke Skywalker", "Darth Vader", "Yoda" ...

  8. Oracle数据库错误消息

    Oracle数据库错误消息 导出错误消息 l EXP-00000导出终止失败 原因:导出时产生Oracle错误. 操作:检查相应的Oracle错误消息. l EXP-00001数据域被截断 - 列长度 ...

  9. linux c开发: 在程序退出时进行处理

    有时候,希望程序退出时能进行一些处理,比如保存状态,释放一些资源.c语言开发的linux程序,有可能正常退出(exit),有可能异常crash,而异常crash可能是响应了某信号的默认处理.这里总结一 ...

  10. centos7部署ethereum私有链

    https://github.com/ethereum/go-ethereum/wiki http://book.8btc.com/books/6/ethereum/_book/public-chai ...