描述&背景
Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存密码等重要的连接信息,此时,唯有继承PopertyPlaceholderConfigurer,并重写convertProperty(String propertyName, String propertyValue)方法,该方法是java中少有的传参承载设计模式,在这里,我们可以拿到我们需要进行解密的密文再传给spring组件去连接数据库,避免了明文保存。所以我们可以将加密后的信息保存到.Properties文件中,在读取前解密,就可以实现不明文保存信息需求。这里我将用AES来加密重要信息。
步骤
1. 导加密工具文件
将AES加密文件放到项目工具类的位置,如有其它加密和解密工具,可不用我这个AES加密。
2. 继承PropertyPlaceholderConfigurer

package com.openeap.common.web;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.openeap.common.utils.aes.AESEncryptor;

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};
private String code = "gzxcxxxtgcyxgs01";
@Override
protected String convertProperty(String propertyName, String propertyValue)
{
//如果在加密属性名单中发现该属性
if (isEncryptProp(propertyName))
{
String decryptValue="";
try {
decryptValue = AESEncryptor.decrypt(code, propertyValue);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        
System.out.println(decryptValue);
return decryptValue==""?propertyValue:decryptValue;
}else {
return propertyValue;
} } private boolean isEncryptProp(String propertyName)
{
for (String encryptName : encryptPropNames)
{
if (encryptName.equals(propertyName))
{
return true;
}
}
return false;
}
}

AES加密,还需要一个code,这里需要一个是16位或者16位配数的密钥当spring读取到带有encryptPropNames包含的字段时,会执行convertProperty方法进行解密
注:.propertites文件中保存的格式为
jdbc.username=admin
jdbc.password=123456

3. Spring配置文件配置
在spring加载属性配置文件时,用

<bean class="com.openeap.common.web.EncryptPropertyPlaceholderConfigurer"  >
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
</list>
</property>
</bean>

替换原来的

<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />

4. 计算加密后的信息
AES加密方法中encrypt(String seek, String cleartext),例如原始值是aaa,密钥是1234567887654321,得到的密码是N!Kk8dwLm0z7hlGkq2dbdQ==

最后将密文信息回填到.properties文件中。

jdbc.username=N!Kk8dwLm0z7hlGkq2dbdQ==

至此,spring不在配置信息里保存重要的明文信息了。

如有错误,恳请指正,不胜感激!

===============我是分割线==============

AES加密文件下载地址

http://pan.baidu.com/s/1jH6bM3W

如失效请邮件通知我,cngdsch@163.com

spring读取加密配置信息的更多相关文章

  1. 【spring boot】配置信息

    ======================================================================== 1.feign 超时配置 2.上传文件大小控制 3.J ...

  2. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  3. spring boot mybatis XML文件读取properties配置信息

    配置文件application.properties中相关配置信息可以在部署以后修改,引用配置信息可以在代码和mybatis的映射文件中 1.JAVA代码 可以通过变量去读取 application. ...

  4. Spring读取加密属性文件处理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  5. Spring读取加密属性文件处理--待整理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  6. PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)

    http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)

  7. 商铺项目(使用DES加密配置信息)

    package com.ouyan.o2o.util; import java.security.Key; import java.security.SecureRandom; import java ...

  8. JDBC通过配置文件(properites)读取数据库配置信息

    扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...

  9. 读取.properties配置信息

    package com.ctcti.webcallcenter.utils; import java.io.FileInputStream;import java.io.FileNotFoundExc ...

随机推荐

  1. HDU 4821 String hash

    String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. 使用CKEditor编辑器进行文本编辑

    首先下载CKEditor. 下载地址:点击打开链接 将下载完的CKEditor解压而且导入到项目中. 然后在页面引入CKEditor <script type="text/javasc ...

  3. xcode 左边导航栏中,类文件后面的标记“A”,&quot;M&quot;,&quot;?&quot;……等符号的含义???

    "M" = Locally modified    "U" = Updated in repository  "A" = Locally a ...

  4. CSS里的单位

    CSS中预设了16种颜色以及16种颜色的衍生色,这16种颜色是CSS规范推荐的.并且一些主流的浏览器都可以识别.例如以下表所看到的: 十六进制颜色是最经常使用的定义方式.它的基本格式为#RRGGBB, ...

  5. Python2.7.3移除字符串中重复字符(一)

    移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...

  6. Android常用代码集合

    这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...

  7. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...

  8. 隐藏gvim中的工具栏和菜单栏

    在vim的配置文件.vimrc中添加如下代码: "Toggle Menu and Toolbar set guioptions-=m set guioptions-=T map <si ...

  9. Python学习 之 正则表达式

    1.简单的正则表达式 import re s=r'abc' re.findall(s,"aaaaaaaaaaaaaaa") #结果为[] re.findall(s,"ab ...

  10. php引用详解

    <?php function foo($bar){   $bar='111111111'; } $str='2222222222222'; foo($str); echo $str; //out ...