描述&背景
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. CSS模块化

    1. Base2. Layout3. Module4. State5. Theme 1) Base rules Base rules are the defaults. eg: ;; } input[ ...

  2. C# 手动读写app config 的源码

    public class ConfigOperator { public string strFileName; public string configName; public string con ...

  3. Github上最全的APICloud开源前端框架效果盘点(转)

    1.微信网站几分钟变身“原生 App” 微信推出了微信JS-SDK,使微信公共号可以直接调用微信原生的接口,具备部分原生应用的能力.微信JS-SDK的推出,将大大提高微信公共号的 用户体验,但是如果存 ...

  4. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  5. Android & Eclipse FAQ

    一.eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法 当我要格式化代码的时候,右键-source-format能够起效,但ctrl+shift+f不好使了. google之后来发 ...

  6. 谷歌chrome浏览器桌面提醒 webkitNotifications

    原创: //点击时开启提醒 $(".message_alert").toggle(function(){ $(".message_alert_tip").htm ...

  7. MySQL查询缓存详解

    一:缓存条件,原理 MySQL Query Cache是用来缓存我们所执行的SELECT语句以及该语句的结果集,MySql在实现Query Cache的具体技术细节上类似典型的KV存储,就是将SELE ...

  8. ( 转转)Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)

    大家好,今天跟大家谈谈Intent的用法. Intent在安卓中主要用于打开另外一个页面,这个页面可能是一个activity也可能是一个应用,也可能是     其它…… 且看下面介绍,总结摘抄网友一些 ...

  9. python学习笔记(三)--条件语句

    Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条 ...

  10. Android(java)学习笔记113:Android编写代码调用Vibrator震动功能(Bug:按下按钮button始终没有震动)

    1.之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; im ...