对项目中关键的配置信息进行加密

我们在搭键一个项目的时候,有一些信息,比如数据库的连接信息通常会放在一个配置文件中,存放的是明码,我们可以对关键信息比如用户名和密码进行加密,就算别人获取了也没有用

加密后的效果

db.username = WnplV/ietfQ=
db.password = j9ZTgFZm4H7r0AteGA6A7A==
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8

一、首先创建一个加密的工具类,对信息进行加密

我用的是DES对称加密

package com.edward.o2o.util;

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);
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(KEY_STR.getBytes());
			generator.init(secureRandom);
			key = generator.generateKey();
			generator = null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	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);
		}
	}


	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) {
		System.out.println(getEncryptString("root"));
		System.out.println(getEncryptString("123456abCD."));
		System.out.println(getEncryptString("wxd7f6c5b8899fba83"));
		System.out.println(getEncryptString("665ae80dba31fc91ab6191e7da4d676d"));
	}
}

通过main方法,把要加密的信息输出到控制台得到密文,再把对应的明文换成加密后的密文

二、创建一个PropertyPlaceholderConfigurer后置处理器,spring配置文件中会用到这个类解析解析数据库的配置参数

package com.edward.o2o.util;

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

public class EncryptPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	private String[] encryptPropNames = { "db.username", "db.password" };

	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
		if (isEncryptProp(propertyName)) {
			String decryptValue = DESUtils.getDecryptString(propertyValue);
			return decryptValue;
		} else {
			return propertyValue;
		}
	}

	private boolean isEncryptProp(String propertyName) {
		for (String encryptpropertyName : encryptPropNames) {
			if (encryptpropertyName.equals(propertyName))
				return true;
		}
		return false;
	}
}

三、把配置文件加载到spring的配置文件中

 <!-- 1.配置数据库相关参数properties的属性:${url} -->
 <!--<context:property-placeholder location="classpath:db.properties"/>-->
 <bean class="com.edward.o2o.util.EncryptPropertyPlaceholderConfigurer">
     <property name="locations">
         <list>
             <value>classpath:db.properties</value>
         </list>
     </property>
     <property name="fileEncoding" value="UTF-8" />
 </bean>

class就是第二步中用于解析配置文件的类

总结:
整个过程就是利用加密的工具类对关键信息加密把原文替换成密文,然后在spring的配置文件中加载需要的配置文件(比如数据库的配置文件),加载过程中PropertyPlaceholderConfigurer后置处理器会调用加密工具类中的解密文件返回明文

PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章