您的位置:首页 > 编程语言 > Java开发

Spring的一种配置文件加密解密

2017-11-21 14:46 441 查看
最近公司查的严格 要求代码方面安全问题很严格,我呢,就一个人整了整代码出现的一下账号密码 ,在网上找了一种实现加密解密的方式。

话不多说,说原理:

很简单:读取配置文件的时候 将密码 解密了 然后 下面这些 拿到的是解密之后的 而配置文件properties中是加密的东西

username=QnbEdnemerw=
password=QnbEdnemerw=


<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />


实现类 就俩 简单:

1.实现字符串加密解密类

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

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) {
// TODO: handle exception
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) {
// TODO: handle exception
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) {
// TODO: handle exception
throw new RuntimeException(e);
}
}

public static void main(String[] args) {//av21WkQRY3o=
String encryptString = getEncryptString("slave");
System.out.println(encryptString);

String decryptString = getDecryptString("QnbEdnemerw=");
System.out.println(decryptString);
}
}


2.Spring内部的类 ,在项目启动是加载配置文件中的内容 ,我们继承此类 重写读取配置文件的方法

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

/**
* 继承PropertyPlaceholderConfigurer定义支持密文版属性的属性配置器
*
* @author moziqi
*
*/
public class EncryptPropertyPlaceholder extends
PropertyPlaceholderConfigurer {
//配置文件中要解密的键值对的key
private String[] encryptPropNames = { "username", "password" };

@Override
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncryptProp(propertyName)) {
String decryptValue = DESUtils.getDecryptString(propertyValue);
System.out.println(propertyName + "解密内容:" + decryptValue);
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;
}

}


最重要的一步:

application.xml中 以前加载配置文件的xml是这样的:

<context:property-placeholder  location="classpath:***.properties"/>


现在 注释掉上面的

加上这个:

//class为自己写的那个类
<bean class="com.ws.util.EncryptPropertyPlaceholder"
p:location="classpath:w_s_dzqd.properties" p:fileEncoding="utf-8" />


很简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: