Spring 对属性文件的加密与解密
一般用于配置密码等敏感信息
解密/加密工具类
package com.baobaotao.placeholder; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom; /**
* 加密/解密工具类
* Created by sherry on 15-6-28.
*/
public class DESUtils {
/*指定加密/解密使用的密匙*/
private static Key key;
/*注意:密匙必须是8的倍数*/
private static String KEY_STR = "myKeykkk";
static {
try {
/*防止Linux下随机生成key*/
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes()); KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
//keyGenerator.init(new SecureRandom(KEY_STR.getBytes()));
keyGenerator.init(secureRandom);
key = keyGenerator.generateKey();
keyGenerator = null;
}catch (Exception e){
throw new RuntimeException(e);
}
} /*对字符串进行DES加密,返回BASE64编码的加密字符串*/
public static String getEncryptString(String string){
BASE64Encoder base64Encoder = new BASE64Encoder();
try {
byte[] strBytes = string.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64Encoder.encode(encryptStrBytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
} /*对BASE64编码的加密字符串进行解密,返回解密后的字符串*/
public static String getDecryptString(String str){
BASE64Decoder base64Decoder = new BASE64Decoder();
try {
byte[] strBytes = base64Decoder.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes,"UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static void main(String[] args) {
args = new String[]{"root","123456"};
String[] result = new String[2];
if (args == null||args.length<1){
System.out.println("请输入要加密的字符串,用空格分割");
}else {
int i = 0;
for (String arg:args){
System.out.println(arg + ":" + getEncryptString(arg));
result[i++] = getEncryptString(arg);
}
}
for (String temp:result){
System.out.println(temp+":"+getDecryptString(temp));
}
}
}
属性编辑器
package com.baobaotao.placeholder; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /**
* Created by sherry on 15-6-28.
*/
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private String[] encryptPropNames = {"username_mysql","password"}; @Override
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncryptProp(propertyName)){
String decryptValue = DESUtils.getDecryptString(propertyValue);
System.out.println("解密结果:"+decryptValue);
return decryptValue;
}else {
System.out.println("无需解密:"+propertyValue);
return propertyValue;
}
} /*判断是否是需要进行加密的属性*/
public boolean isEncryptProp(String propertyName){
for (String encryptpropertyName:encryptPropNames){
if (encryptpropertyName.equals(propertyName)){
return true;
}
}
return false;
}
}
配置使用
<bean class="com.baobaotao.placeholder.EncryptPropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"
p:fileEncoding="UTF-8"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${username_mysql}"
p:password="${password}"/>
Spring 对属性文件的加密与解密的更多相关文章
- Spring的属性文件properties使用注意
Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- Spring对外部属性文件指定的某个属性进行加密、解密
[From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...
- Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- Java实现文件的加密与解密
最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...
- Spring MVC 属性文件读取注入到静态字段
目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用 在项目中,有些参数需要配置到属性文件xxx.pr ...
- Spring配置属性文件
在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能
Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...
随机推荐
- sudo 密码超时时间
Centos 没有默认超时时间,所以用一次sudo就需要输入密码. vi /etc/sudoers 添加下面的内容,2表示分钟数(看自己需求更改). Defaults timestamp_timeou ...
- windows 编译安卓iconv 库
由于NDK r15后,谷歌要统一将来的设备都要支持64位,而iconv只支持32位,后续的ndk都会去除iconv的支持,所以只能在iconv的官网下载源码编译库文件使用, 下载地址:https:// ...
- Python 创建项目、应用
1.创建项目 django-admin startproject TestPython 2.创建应用 python3 manage.py startapp books 3.目录讲解 ├── TestP ...
- react中实现原生enter/回车事件及antdesign组件实现方式
先直接上核心代码: this.goToHomePage换成自己逻辑 自己写的时候直接把this.goToHmoPage()换成自己的逻辑就行了,还有注意一点的是: 需要传个空函数,不然会报错 在com ...
- 【ACM之行】◇第一站◇ 2018HDU多校赛总结
◇第一站◇ 2018HDU多校赛 十场多校赛下来,也算是给一个初中生开了眼界……看着清华一次次AK(默默立下flag),看着自己被同校的高中生完虐,一个蒟蒻只能给dalao们垫脚
- mysql 数据库设计规范
MySQL数据库设计规范 目录 1. 规范背景与目的 2. 设计规范 2.1 数据库设计 2.1.1 库名 2.1.2 表结构 2.1.3 列数据类型优化 2.1.4 索引设计 2.1.5 分库分表. ...
- zip压缩工具,unzip解压缩工具
zip压缩工具,unzip解压缩工具=================== [root@aminglinux tmp]# yum install -y zip[root@aminglinux tmp] ...
- SQLSERVER 数据库恢复挂起的解决办法
如果你的数据库还处于挂起状态,请把我下面代码的test改为你的库名,然后执行完,刷新就正常了: USE masterGOALTER DATABASE test SET SINGLE_USERGOALT ...
- python如果想输出原格式的内容,可以加''' ''',占位符使用方式
print('我考了%d分'%20) msg=''' ---------info of %s----------- name: %s age: %d #字符串不能放到%d处 job: %s salar ...
- python基础之继承实现原理、子类调用父类的方法、封装
继承实现原理 python中的类可以同时继承多个父类,继承的顺序有两种:深度优先和广度优先. 一般来讲,经典类在多继承的情况下会按照深度优先的方式查找,新式类会按照广度优先的方式查找 示例解析: 没有 ...