对于保密级别较高的公司会有安全检测,其中一项就是禁止出现明文密码,配置文件也不例外-例如:华为(苦逼)
本文针对于spring读取jdbc的配置文件进行加密
1首先你要先有自己的一个加密解密的类
本人用的md5,贴出来供大家参考,不喜欢可以换别的加密,无影响
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* 对配置文件加密解密的类
* encrypt加密方法
* decrypt解密方法
* @author shijiuzhou
*
*/
public class MD5Utils {
private static final String PASSWORD_CRYPT_KEY = "xxxxxxx";
private final static String DES = "xxx";
/**
*
* 加密
*
* @param src
* 数据源
*
* @param key
* 密钥,长度必须是8的倍数
*
* @return 返回加密后的数据
*
* @throws Exception
*/
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(src);
}
/**
*
* 解密
*
* @param src
* 数据源
*
* @param key
* 密钥,长度必须是8的倍数
*
* @return 返回解密后的原始数据
*
* @throws Exception
*/
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 现在,获取数据并解密
// 正式执行解密操作
return cipher.doFinal(src);
}
/**
*
* 密码解密
*
* @param data
*
* @return
*
* @throws Exception
*/
public final static String decrypt(String data) {
try {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
} catch (Exception e) {
}
return null;
}
/**
*
* 密码加密
*
* @param password
*
* @return
*
* @throws Exception
*/
public final static String encrypt(String password) {
try {
return byte2hex(encrypt(password.getBytes(),
PASSWORD_CRYPT_KEY.getBytes()));
} catch (Exception e) {
}
return null;
}
/**
*
* 二行制转字符串
*
* @param b
*
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static void main(String args[]) {
String jami = MD5Utils.encrypt("123");
System.out.println(jami);
String jiemi = MD5Utils.decrypt(jami);
System.out.println(jiemi);
}
}
2.对你的jdbc的配置问价指定的参数加密
本次改的参数为pwd
3.实现spring提供的父类,重写其中的方法
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 继承spring框架的父类实现对配置文件的指定值解密
* @author lenovo
*
*/
public class EncryptPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
@Override
protected String convertProperty(String propertyName, String propertyValue) {
//判断传入的参数key是否需要解密过程
if (isEncryptProp(propertyName)) {
//对指定值进行解密
String decryptValue = MD5Utils.decrypt(propertyValue);
//返回解密结果
return decryptValue;
} else {
//返回不需要解密的值
return propertyValue;
}
}
/**
*
* 判断是否是加密的属性
* pwd是上面jdbc.properties指定的加密参数
* 当然也可以多个参数判定,换一种判断是否加密的
* 方法就可以了,例如遍历数组
*
* @param propertyName
*
* @return
*/
private boolean isEncryptProp(String propertyName) {
if ("pwd".equals(propertyName)){
return true;
}
return false;
}
}
4.正确配置ApplicationContext-dao.xml(你的spring配置dao层的配置文件)
配置数据源的代码
<!-- <import resource="config/*.xml"/> -->
<!-- 加载指定的属性文件 -->
<!-- 不加密配制方法 -->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<!-- 加密的配制方法 -->
<bean class="com.yx.web.utils.conf.encrypt.EncryptPropertyPlaceholderConfigurer" >
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
bean的class类是第三步的class类