Spring读取加密属性文件处理
来源:互联网 发布:淘宝服装店店铺介绍 编辑:程序博客网 时间:2024/06/05 11:23
Spring读取加密属性文件处理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器。Spring的依赖、注入、AOP及和其它框架的很好集成(如:hibername、ibatis、struts等)确实给web项目开发带来了诸多便利性,但是任何一种框架都不能完全满足个性化需求开发,spring亦是如此。现有一个项目是基于spring、struts和ibtatis的,其中数据库连接池使用的是proxool,领导要求将proxool连接池配置文件进行加密,这里有2种解决方法:
1) 扩展ProxoolDataSource,重写getNewConnection方法,对其置相关数据库配置属性时进行解密处理;
2) 扩展Spring读取属性文件文件的类PropertyPlaceholderConfigurer
1、 扩展ProxoolDataSource
package *.*; import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties; import org.logicalcobwebs.proxool.ProxoolDataSource; public class ProxoolDataSourceEX extends ProxoolDataSource { private Logger errorLog = CommonLogger.getErrorLog(ProxoolDataSourceEX.class); private static Properties proxoolProperties = null; private static ProxoolDataSource dataSource = null; // public synchronized Connection getConnection() { try { if (dataSource != null) return super.getConnection(); else return getNewConnection(); } catch (SQLException e) {// errorLog.error("…….", e); } return null; } private synchronized Connection getNewConnection() { if(proxoolProperties==null){ InputStream is = Thread.currentThread().getContextClassLoader(). getResourceAsStream("proxool.properties"); proxoolProperties = new Properties(); try{ proxoolProperties.load(is); }catch(Exception e){ e.printStackTrace(); } } //属性值的解密(调用相应解密算法,解密) //解密后的属性值置入 this.setDriver(driver); this.setDriverUrl(url); … try { return super.getConnection(); } catch (SQLException e) { errorLog.error("…", e); } return null; }}
2、 扩展Spring读取属性文件文件的类PropertyPlaceholderConfigurer
1) spring datasource配置
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- ======================================================================== --> <!-- DataSource定义。 --> <!-- ======================================================================== --> <bean id="DBConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:proxool.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver"> <value>${dev_proxool_driver_class}</value> </property> <property name="driverUrl"> <value>${dev_proxool_driver_url}</value> </property> <property name="user"> <value>${dev_proxool_user}</value> </property> <property name="password"> <value>${dev_proxool_password}</value> </property> <property name="alias"> <value>${dev_proxool_alias}</value> </property> … </bean> …</beans>
2) 扩展PropertyPlaceholderConfigurer,对其方法resolvePlaceholder进行重写。
package *.*; import java.util.Properties; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class PropertyPlaceholderConfigurerEX extends PropertyPlaceholderConfigurer{ private boolean secutiry = false; private Log logger = LogFactory.getLog(PropertyPlaceholderConfigurerEX.class); // protected String resolvePlaceholder(String placeholder, Properties props) { String placeholderValue = props.getProperty(placeholder); if(this.secutiry){ placeholderValue = deEncrypt(placeholderValue); } return placeholderValue; } // public boolean isSecutiry() { return secutiry; } public void setSecutiry(boolean secutiry) { this.secutiry = secutiry; } private String deEncrypt(String miwen){ return 解密后的字串; }}
3) 修改上述的datasource配置
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- ======================================================================== --> <!-- DataSource定义。 --> <!-- ======================================================================== --> <bean id="DBConfigurer" class="*.*.PropertyPlaceholderConfigurerEX"> <property name="locations"> <list> <value>classpath:proxool.properties</value> </list> </property> <!—security为false,则对属性文件的属性值不进行解密处理,为true,则进行解密--> <property name="secutiry"> <value>false</value> </property> <!—扩展PropertyPlaceholderConfigurerEX,最好使用解密算法也可在此处配置--> </bean> 同1)datasource配置
转载地址:http://blog.csdn.net/chuangxin/article/details/5039242
- Spring读取加密属性文件处理
- Spring读取加密属性文件处理
- 使用PropertyPlaceholderConfigurer读取.properties文件(2)-处理加密属性
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- spring 读取属性文件
- Spring读取属性文件
- java文件加密|spring属性文件加密
- spring 读取properties属性文件
- Spring读取properties文件加密解密
- Spring对属性文件加密解密应用
- spring使用加密的属性文件
- Spring-使用加密的属性文件02
- spring里PropertyPlaceholderConfigurer读取属性文件
- Spring 读取属性文件的便捷方式
- spring中属性文件properties的读取
- 未能恢复-无效的自变量“的解决方法 (GUID分区)LION
- Python中的图
- Struts2 <s:iterator> 标签实现 for 循环式 循环控制
- 又见一帘幽梦
- mysql时间处理函数
- Spring读取加密属性文件处理
- Linux C++ 字符串 编码识别、编码转换
- 如何oracle修改字段类型
- java内存空间详解
- 系统集成项目管理工程师笔记(17)信息系统安全管理(18) 项目风险管理(19)项目收尾管理
- 如何阅读源代码
- TinyXml使用与剖析
- poj1804
- go 语言入门