加密Spring加载的Properties文件

实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密。
一,编写工具类,继承PropertyPlaceholderConfigurer

package com.csair.cbd.utils;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.csair.cbd.member.utils.CryptUtils;
import com.csair.common.exceptions.ServiceException;

/**
* 配置文件解密类
* @author liuyouyi
* @company csair
* 2013-10-21
* sybase
* com.csair.cbd.utils
* @version 1.0
*
*/
public class MyConfigurer extends PropertyPlaceholderConfigurer {

public final static String strKey = "4321aaaaaaaa1314aaaaalyy";
private static final Log logger = LogFactory.getLog(MyConfigurer.class);

@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {



String jdbcPassword = props.getProperty("jdbc.password");
if(jdbcPassword != null ) {
try {
//logger.info("----------------------------加密前-----------------------------");
props.setProperty("jdbc.password", decrypt(jdbcPassword));
//logger.info("----------------------------加密后-----------------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
String jdbc2Password = props.getProperty("jdbc2.password");
if(jdbc2Password != null ) {
try {
props.setProperty("jdbc2.password", decrypt(jdbc2Password));
} catch (Exception e) {
e.printStackTrace();
}
}

String iqjdbcPassword = props.getProperty("iqjdbc.password");
if(iqjdbcPassword != null ) {
try {
props.setProperty("iqjdbc.password", decrypt(iqjdbcPassword));
} catch (Exception e) {
e.printStackTrace();
}
}

String jdbcMysqlPassword = props.getProperty("jdbc.mysql.password");
if(jdbcMysqlPassword != null ) {
try {
props.setProperty("jdbc.mysql.password", decrypt(jdbcMysqlPassword));
} catch (Exception e) {
e.printStackTrace();
}
}

String mysqlJdbcPassword = props.getProperty("mysql.jdbc.password");
if(mysqlJdbcPassword != null ) {
try {
props.setProperty("mysql.jdbc.password", decrypt(mysqlJdbcPassword));
} catch (Exception e) {
e.printStackTrace();
}
}

super.processProperties(beanFactory, props);

}

/**
* 加密
*
* @param password
*/
public static String encrypt(String password) throws Exception {

// SHA1加密
String uid = CryptUtils.getInstance(CryptUtils.Algorithm_DESede)
.encrypt(password, strKey);
return uid;
}

/**
* 解密
*
* @param password
*/
public static String decrypt(String password) throws ServiceException {
try {
return CryptUtils.getInstance(CryptUtils.Algorithm_DESede).decrypt(
password, strKey);
} catch (Exception e) {
throw new ServiceException("解密失败!");
}
}


public static void main(String[] args) {
try {
String string1 = encrypt("test_password");
String string2 = decrypt(string1);
//String string3 = ReserverCookie.decode(string2);
System.out.println(string1+"--"+string2);
} catch (Exception e) {
}
}

}



二,改写配置文件

<bean id="JDBCpropertyConfigurer"
class="com.csair.cbd.utils.MyConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:system.properties</value>
<value>classpath:sms-template.properties</value>
<value>classpath:deploy/test.properties</value>
<value>classpath:com/csair/cbd/member/member-verify.properties</value>
<value>classpath:com/csair/cbd/rule/rule-message.properties</value>
<value>classpath:com/csair/cbd/integral/services/impl/deferregist-message.properties</value>
</list>
</property>
</bean>


三,整合其它的PropertyPlaceholderConfigurer配置,以免被覆盖
四,去掉context:property-placeholder,否则配置也是会被覆盖的

<!--context:property-placeholder location="classpath:jdbc.properties,classpath:system.properties,classpath:sms-template.properties,classpath:deploy/test.properties,classpath:com/csair/cbd/member/member-verify.properties,classpath:com/csair/cbd/rule/rule-message.properties"/-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值