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. C# 定制特性

    一.初识特性 特性(attribute)是被指定给某一声明的一则附加的声明性信息. 设计类型的时候可以使用各种成员来描述该类型的信息,但有时候我们可能不太愿意将一些附加信息放到类的内部,因为这样,可能 ...

  2. 利用request、beautifulsoup、xml写多线程爬虫

    # -*- coding:UTF-8 -*- import requests,time from collections import OrderedDict import threading fro ...

  3. git pull冲突报错

    修改代码后在git pull报错: error: Your local changes to the following files would be overwritten by merge: xx ...

  4. [转]MySQL索引方法

    此为转载文章,仅做记录使用,方便日后查看,原文链接:https://www.cnblogs.com/luyucheng/p/6289048.html MySQL索引方法   MySQL目前主要有以下几 ...

  5. java输出九九乘法口诀表

    使用双重for循环输出九九乘法口诀表 public static void main(String[] args){ formula();} /** * for 循环实现9*9乘法口诀表 * &quo ...

  6. C#学习笔记4

    1.C#只支持单一继承,若想要实现多重继承的效果.有2种方式: 第一种为传递继承,A为基类,B继承A,C继承B,通过传递来增迭要包含的元素,但这种继承的设计,在业务上具有明显的从属关系. 第二种为聚合 ...

  7. javascript: iframe switchSysBar 左欄打開關閉,兼容各瀏覽器操作

    <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Con ...

  8. Vue element-ui:滚动条 分页 禁用选项

    1.滚动条设置: <el-scrollbar style="height:100%;">.......</el-scrollbar> 默认会同时出现水平和垂 ...

  9. 2729:Blah数集

    2729:Blah数集 查看 提交 统计 提问 总时间限制: 3000ms 内存限制: 65536kB 描述 大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下: ...

  10. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...