1 加密工具类
可以使用自己的加密解密工具替换encrypt(String encText)和decrypt(String decText)方法就行了。
public class ConfigCryptoUtil {
// 这个密码需要修改为自己的密码
private final static String PASSWORD = "password";
// 加密算法
private final static String ALGORITHM = "PBEWithMD5AndDES";
// 盐
private final static byte[] SALT = "63293188".getBytes();
// 迭代次数
private final static int INTERATION_COUNT = 1000;
private static Key getPBEKey() {
SecretKeyFactory keyFactory;
SecretKey key = null;
PBEKeySpec keySpec = new PBEKeySpec(PASSWORD.toCharArray());
try {
keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
key = keyFactory.generateSecret(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
/**
* 加密
*
* @param encText
* 要加密文本
* @return
*/
public static String encrypt(String encText) {
Key key = getPBEKey();
byte[] encTextByte = null;
PBEParameterSpec parameterSpec = new PBEParameterSpec(SALT, INTERATION_COUNT);
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
encTextByte = cipher.doFinal(encText.getBytes());
} catch (Exception e) {
}
return bytesToHexString(encTextByte);
}
/**
* 解密
*
* @param decText
* 要解密文本
* @return
*/
public static String decrypt(String decText) {
Key key = getPBEKey();
byte[] decTextByte = null;
PBEParameterSpec parameterSpec = new PBEParameterSpec(SALT, INTERATION_COUNT);
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
decTextByte = cipher.doFinal(hexStringToBytes(decText));
} catch (Exception e) {
}
return new String(decTextByte);
}
// 以下代码百度
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
// 以下代码百度
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
// 以下代码百度
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static void main(String[] args) {
String encText = "我是要加密字符串";
String decText = encrypt(encText);
System.out.println("密文:" + decText);
String org = decrypt(decText);
System.out.println("明文:" + org);
}
}
2 继承PropertyPlaceholderConfigurer
已经加密的配置信息需要在decryptNames中指定,这样才会进行解密
public class CryptoPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
/**
* 需要被解密字段
*/
private String[] decryptNames;
public CryptoPlaceholderConfigurer() {
}
// spring-3.x以上
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncrypt(propertyName)) {
return ConfigCryptoUtil.decrypt(propertyValue);
}
return propertyValue;
}
/**
* 该属性是否被加密
*
* @param propertyName
* @return
*/
private boolean isEncrypt(String propertyName) {
if (decryptNames != null && decryptNames.length > 0)
for (String name : decryptNames) {
if (name != null && name.equals(propertyName))
return true;
}
return false;
}
public String[] getDecryptNames() {
return decryptNames;
}
public void setDecryptNames(String[] decryptNames) {
this.decryptNames = decryptNames;
}
}
3 Spring Bean配置
所有的配置文件都可以使用CryptoPlaceholderConfigurer来读取,需要解密的放到decryptNames中
<bean class="junfeng.config.CryptoPlaceholderConfigurer">
<property name="location" value="classpath:dbconfig.properties" />
<!-- <property name="decryptNames">
<array>
<value>jdbc.url</value>
<value>jdbc.username</value>
<value>jdbc.password</value>
</array>
</property> -->
</bean>
4 dbconfig.properties配置文件
以下是没有加密的情况,加密后 jdbc.url=xxxx密文xxxxx
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
搞定了!
源代码点我下载