一般用于配置密码等敏感信息

解密/加密工具类

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 对属性文件的加密与解密的更多相关文章

  1. Spring的属性文件properties使用注意

    Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...

  2. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  3. Spring对外部属性文件指定的某个属性进行加密、解密

    [From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...

  4. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  5. Java实现文件的加密与解密

    最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...

  6. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  7. Spring配置属性文件

    在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...

  8. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  9. Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能

    Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...

随机推荐

  1. HTML第三章:表单

    第三章:表单 表单标签form:<form></form>//相当于一张记录用户信息的单子    常用属性:method:表单的提交方式,常用的值有两个             ...

  2. Linux更改ssh端口号,很easy!

    因为公司业务需求,可能涉及到更改ssh远程的端口号,用下面方法轻松解决,废话不多说! 1.打开ssh端口配置文件:vim /etc/ssh/sshd_config,找到如下图所示的端口,改为自己想改的 ...

  3. 知识总结和记录——HTML

    文档结构 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...

  4. Windows下MySQL数据库的安装与关闭开机自启动

    我在学习java,安装数据库时找了很多教程,现在在这里总结一下我安装数据库的过程,我安装的是mysql-5.6.42-winx64版本的. 数据官方下载地址:https://dev.mysql.com ...

  5. js | javascript改变style样式和css样式

    转载 在很多情况下,都需要对网页上元素的样式进行动态的修改.在JavaScript中提供几种方式动态的修改样式,下面将介绍方法的使用.效果.以及缺陷. 1.使用obj.className来修改样式表的 ...

  6. MySQL运行一段时间后自动停止问题的排查

    在进入主题前,一定要先吐槽下自己,前段时间购买了一台阿里云服务器,最开始打算只是自己个人用的,就买了一台配置很寒碜的服务器: CPU: 1核 内存: 1 GB 操作系统: CentOS 7.2 64位 ...

  7. 词向量1.md

    词向量 我们以句子分类为例,我们使用深度学习模型对句子进行分类,本质上这个模型的接受的舒服需要是数值型.因为文字是人们抽象出来的一个概念,这个 东西是不能被计算机直接理解的,我们需要人为的将这个文字转 ...

  8. Python学习之函数参数

    上一节,我们学习了Python中是如何定义和调用函数且如何得到返回值的.在调用函数时,有的函数需要参数来启动函数,有的则无需参数.这一节我们来介绍Python中有哪些参数类型. 位置参数 在调用函数时 ...

  9. Trident学习笔记(一)

    1. Trident入门 Trident ------------------- 三叉戟 storm高级抽象,支持有状态流处理: 好处是确保消费被处理一次: 以小批次方式处理输入流,得到精准一次性处理 ...

  10. centos使用--排查服务是否可用

    端口与服务的关系 一台拥有IP地址的主机可以提供许多服务,比如Web服务.FTP服务.SMTP服务等,这些服务完全通过1个IP地址来实现.那么,主机是怎样区分不同的网络服务呢?显然不能只靠IP地址,因 ...