spring使用加密的属性文件
来源:互联网 发布:linux chgrp chown 编辑:程序博客网 时间:2024/06/11 12:59
des加密工具类
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"; static { try { KeyGenerator generator = KeyGenerator.getInstance("DES"); 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 base64en = new BASE64Encoder(); try { byte[] strBytes = str.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptStrBytes = cipher.doFinal(strBytes); return base64en.encode(encryptStrBytes); } catch (Exception e) { throw new RuntimeException(e); } } /** * 对str进行DES解密 * * @param str * @return */ public static String getDecryptString(String str) { BASE64Decoder base64De = new BASE64Decoder(); try { byte[] strBytes = base64De.decodeBuffer(str); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptStrBytes = cipher.doFinal(strBytes); return new String(decryptStrBytes, "UTF8"); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { if (args == null || args.length < 1) { System.out.println("请输入要加密的字符,用空格分隔."); } else { for (String arg : args) { System.out.println(arg + ":" + getEncryptString(arg)); } } //System.out.println(getDecryptString("WnplV/ietfQ=")); //System.out.println(getDecryptString("gJQ9O+q34qk=")); }}
加密用户名密码
在控制台执行java DESUtils root 123456得到加密后的字符串,用于配置属性文件
扩展PropertyPlaceholderConfigurer类
mport org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;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); System.out.println(decryptValue); return decryptValue; }else{ return propertyValue; } } /** * 判断是否是加密的属性 * @param propertyName * @return */ private boolean isEncryptProp(String propertyName){ for(String encryptPropName:encryptPropNames){ if(encryptPropName.equals(propertyName)){ return true; } } return false; }}
0 0
- spring使用加密的属性文件
- Spring-使用加密的属性文件02
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- java文件加密|spring属性文件加密
- 在Spring中如何使用加密外部属性文件
- Spring读取加密属性文件处理
- Spring对属性文件加密解密应用
- Spring读取加密属性文件处理
- Spring对外部属性文件指定的某个属性进行加密、解密
- Spring对外部属性文件指定的某个属性进行加密、解密
- SPRING使用占位符读取字段加密的properties文件
- spring 使用外部属性文件
- Spring 使用外部属性文件
- Spring使用外部属性文件
- Spring使用外部属性文件
- spring-使用外部属性文件
- Spring 使用外部属性文件
- spring使用外部属性文件
- [LeetCode]40.Combination Sum II
- 操作系统的进程、多线程、内存管理、垃圾回收、以及缓冲等方面
- 内存优化之内存泄漏问题
- USACO 3.1 Score Inflation (完全背包) P2722 总分 Score Inflation
- java中Iterator有什么用,举例说明?
- spring使用加密的属性文件
- oracle 除数为 0
- 杭电oj(Java版)——2005 第几天?
- CodeForces 204D Little Elephant and Retro Strings
- Android应用开发之数据加密浅析
- PL/SQL 异常处理
- Volley源码解析
- HTML5、CSS雪碧图
- 杭电oj(Java版)——2006 求奇数的乘积