项目中关于配置文件中密码的加密处理
转载地址:http://supanccy2013.iteye.com/blog/2101964
在项目中,为了项目的配置灵活,长把一些常量写在配置文件中,这时涉及一个问题,就是如果配置字段是密码,就不够安全。这个时候需要在配置文件中把密码加密处理。下面是处理方案:
实际遇到的有两种情况,一种是自己写程序来都去配置文件,这个时候处理比较简单,把自己的加密工具,把密码加个密放进配置文件中,读取的时候再解密即可。 第二种情况是遇到框架东西,例如spring这种框架,密码加密之后卸载配置文件中,我们该怎么处理:下面是处理方法:
1,用自己的加密工具把密码加密之后的密文卸载spring配置文件中。
2,自己写一个读取配置文件的类,这个类必须实现spring的一个接口,并实现当中的相关方法,把这个类配置到spring的配置文件中,并且配置的时候这个类的id必须是固定的,这样spring才能用你写的集成类读取配置文件。
下面是实例:
1,手写新建一个properties的配置文件,放在src的根目录下,内容如下:
Java代码 收藏代码
driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
user=supan
password=root
maxPoolSize=100
maxIdleTime=100
maxStatementsPerConnection=100
numHelperThreads=1000
idleConnectionTestPeriod=30
2,自己写一个读取配置文件的类,该类必须继承spring的接口。内容如下:
Java代码 收藏代码
package com.supan.frame;
import java.util.Enumeration;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.ObjectUtils;
public class MyPropertyPlaceConfigurer extends PropertyPlaceholderConfigurer
{
@Override
protected void convertProperties(Properties props)
{
super.convertProperties(props);
//读取properties文件中键的名字
Enumeration<?> propertyNames = props.propertyNames();
//遍历键名字
while(propertyNames.hasMoreElements())
{
String propertyName = (String)propertyNames.nextElement();
//配置的值
String propertyValue = props.getProperty(propertyName);
//转换后的值
String convertedValue = convertPropertyValue(propertyValue);
//对名为password的值进行解密
if("password".equals(propertyName))
{
//在这里可以用自己的加密工具对配置的密文进行解密
//因为此时没有加密工具只是演示一下。
// convertedValue = EmfCipher.decrpypt(convertedValue);
convertedValue = "root";
}
if(ObjectUtils.nullSafeEquals(propertyValue, convertedValue))
{
//注意:这里并不是去修改配置文件,而是改变读取到配置文件中的键值对
props.setProperty(propertyName, convertedValue);
}
}
}
}
3,把自己写的读取配置文件的java类配置到配置文件中。注意配置读取bean的id一定要是propertyConfigurer
Java代码 收藏代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--注入配置文件读取bean -->
<bean id="propertyConfigurer" class="com.supan.frame.MyPropertyPlaceConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${jdbcUrl}</value>
</property>
<property name="user">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<property name="maxPoolSize">
<value>${maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${maxIdleTime}</value>
</property>
<property name="maxStatementsPerConnection">
<value>${maxStatementsPerConnection}</value>
</property>
<property name="numHelperThreads">
<value>${numHelperThreads}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${idleConnectionTestPeriod}</value>
</property>
</bean>
<!-- 定义HIbernate的SessionFactory,让Spring管理HIbernate,实现Spring和hibernate的整合 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/supan/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.supan.dao.imp.UserDaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
4,userDao实现类
Java代码 收藏代码
public void getUserNameAndInfo()
{
//定义存放结果的结果map
final Map<String,String> result = new HashMap<String, String>();
getHibernateTemplate().execute(new HibernateCallback<Object>()
{
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException
{
session.doWork(new Work()
{
@Override
public void execute(Connection conn) throws SQLException
{
CallableStatement proc = null;
try
{
proc = conn.prepareCall("{call PROC_GETUSER_NAME_AGE(?,?,?)}");
//注意:这里是注册输出参数
proc.registerOutParameter(1, java.sql.Types.VARCHAR);
proc.registerOutParameter(2, java.sql.Types.VARCHAR);
//注意:这里是传递输入参数
proc.setLong(3, 21L);
//执行存储过程
proc.execute();
//获取执行完的存储过程的返回值
result.put("name", proc.getString(1));
result.put("age", proc.getString(2));
}
catch(Exception e)
{
//logger.error("访问数据库失败");
e.printStackTrace();
result.put("name", null);
result.put("age", null);
}
finally
{
if(null != proc)
{
proc.close();
}
}
}
});
return null;
}
});
System.out.println(result.get("name"));
System.out.println(result.get("age"));
}
5,测试类
Java代码 收藏代码
package com.supan.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.supan.dao.UserDao;
import junit.framework.TestCase;
public class JunitMainTest extends TestCase
{
public void testApringAndHibernate()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao ud = (UserDao)ctx.getBean("userDao");
ud.getUserNameAndInfo();
}
}
6,创建数据库表,并插入一条数据
-- Create table
create table TBL_USER
(
ID NUMBER(10) not null,
NAME VARCHAR2(20 CHAR),
INFO VARCHAR2(30 CHAR),
REMARK VARCHAR2(30 CHAR),
AGE VARCHAR2(3 CHAR)
);
insert into tbl_user values(hibernate_sequence.nextval,'chenchaoyang','is a good man','hahha','26');
7,存储过程
Java代码 收藏代码
/*创建存储过程,该存储过程三个参数,前两个是输出参数
最后一个是输入参数*/
create or replace procedure PROC_GETUSER_NAME_AGE(userName out varchar2,
userAge out varchar2,
userId in long)
AS
--声明该存储过程为“自治事物单元”
PRAGMA AUTONOMOUS_TRANSACTION;
--定义两个变量
v_userName varchar2(255);
v_userAge varchar2(255);
begin
select name, info into v_userName, v_userAge from TBL_User t where t.id = userId;
userName := v_userName;
userAge := v_userAge;
end;
8,输出结果:
chenchaoyang
is a good man