spring中的数据源配置信息加密方案

关键字:spring中的数据源配置信息加密方案

[color=red]附件是完整的实现及说明[/color]

在使用spring框架开发一些核心的系统时经常会收到客户提出重要信息加密的要求,在这里我就记录一下我实现Spring数据源配置信息JDBC属性的加密过程。
工具/原料

JAVA开发环境
Spring+mybatis+struts2

方法/步骤

先在项目中新建一个加密算法类,代码如下:

package com.cttsp.frame.util.encrypt;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

* DES加密算法工具类

* @author RyanCai

* @date 2014-10-8 下午3:24:14

*/

public class DESUtils {

private static Key key;

private static String KEY_STR = "myKey";// 密钥

private static String CHARSETNAME = "UTF-8";// 编码

private static String ALGORITHM = "DES";// 加密类型



static {

try {

KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);

generator.init(new SecureRandom(KEY_STR.getBytes()));

key = generator.generateKey();

generator = null;

} catch (Exception e) {

throw new RuntimeException(e);

}

}



/**

* 对str进行DES加密

*

* @param str

* @return

*/

public static String getEncryptString(String str) {

BASE64Encoder base64encoder = new BASE64Encoder();

try {

byte[] bytes = str.getBytes(CHARSETNAME);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] doFinal = cipher.doFinal(bytes);

return base64encoder.encode(doFinal);

} catch (Exception e) {

throw new RuntimeException(e);

}

}



/**

* 对str进行DES解密

*

* @param str

* @return

*/

public static String getDecryptString(String str) {

BASE64Decoder base64decoder = new BASE64Decoder();

try {

byte[] bytes = base64decoder.decodeBuffer(str);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] doFinal = cipher.doFinal(bytes);

return new String(doFinal, CHARSETNAME);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

}

继承实现一个加密配置属性处理类,代码如下:

package com.cttsp.frame.util.encrypt;

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

/**

* 继承PropertyPlaceholderConfigurer支持密文属性的属性配置器

* @author <姓名>

* @date 2014-10-8 下午3:34:14

*/

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

private String[] encryptPropNames = { "username", "password" };



@Override

protected String convertProperty(String propertyName, String propertyValue) {

if (isEncryptProp(propertyName)) {

String decryptValue = DESUtils.getDecryptString(propertyValue);

return decryptValue;

} else {

return propertyValue;

}

}



/**

* 判断是否是加密的属性

*

* @param propertyName

* @return

*/

private boolean isEncryptProp(String propertyName) {

for (String encryptpropertyName : encryptPropNames) {

if (encryptpropertyName.equals(propertyName))

return true;

}

return false;

}

}

修改自己的jdbc属性文件,将需要加密的信息改成加密后的密文,我的配置文件如下图:
对spring中的数据源配置信息加密

再修改srping的配置文件,主要修改如下截图:
对spring中的数据源配置信息加密
5

到这里所有的修改基本上完成了,启动项目正常连接数据库。


[color=red]附件是完整的实现及说明[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值