Spring的一种配置文件加密解密
2017-11-21 14:46
441 查看
最近公司查的严格 要求代码方面安全问题很严格,我呢,就一个人整了整代码出现的一下账号密码 ,在网上找了一种实现加密解密的方式。
话不多说,说原理:
很简单:读取配置文件的时候 将密码 解密了 然后 下面这些 拿到的是解密之后的 而配置文件properties中是加密的东西
实现类 就俩 简单:
1.实现字符串加密解密类
2.Spring内部的类 ,在项目启动是加载配置文件中的内容 ,我们继承此类 重写读取配置文件的方法
最重要的一步:
application.xml中 以前加载配置文件的xml是这样的:
现在 注释掉上面的
加上这个:
很简单
话不多说,说原理:
很简单:读取配置文件的时候 将密码 解密了 然后 下面这些 拿到的是解密之后的 而配置文件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" />
很简单
相关文章推荐
- Spring cloud config配置文件加密解密
- Spring加载Properties配置文件的加密解密处理
- spring读取配置文件加密/解密字段并在XML文件中用${key}传入修改后的配置
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- web.config文件中配置节的加密与解密
- c# web.config 配置文件的加密与解密,aspnet_regiis.exe
- spring-jdbc文件数据库配置加密
- ASP.NET2.0中配置文件的加密与解密
- Spring Boot: 加密应用配置文件敏感信息
- Spring加载加密的配置文件详解
- 给hibernate配置文件加密解密的方案
- spring boot 在不同环境下读取不同配置文件的一种方式
- spring下jdbc配置文件进行加密解密
- java spring中对properties属性文件加密及其解密
- spring配置文件加密
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- Spring对属性文件加密解密应用详解