引言: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配置

Spring读取加密属性文件处理的更多相关文章

  1. Spring读取加密属性文件处理--待整理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  2. Java学习笔记——JDBC读取properties属性文件

    Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...

  3. spring读取加密配置信息

    描述&背景Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存 ...

  4. Spring读取外部属性-properties

    概述 在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取.比如数据源信息配置,Java固定属性位置等.读取的方式一般由三种: ...

  5. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  6. 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

  7. Spring中使用属性文件properties的两种方式

    实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...

  8. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  9. Spring使用外部属性文件

    一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...

随机推荐

  1. hdu 5056 Boring count

    贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...

  2. 每天一个linux命令(1):more命令

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  3. html+ashx 缓存问题

    最近采用html+ashx的方式做了一个项目的几个配置页面的功能,由于浏览器的缓存问题,每次更新数据提交后,页面总是不会刷新,也就是说除了第一次加载页面会向一般处理(ashx)拿数据外,其他情况都是优 ...

  4. LBS地理位置距离计算方法之geohash算法

    随着移动终端的普及,很多应用都基于LBS功能,附近的某某(餐馆.银行.妹纸等等).基础数据中,一般保存了目标位置的经纬度:利用用户提供的经纬度,进行对比,从而获得是否在附近.这里需要在设置出一个字段, ...

  5. 解决Genemotion 安装出现“Unable to start......”的问题

    最近在用uiautomator做安卓自动化测试,由于没有测试设备,所以只好自己在电脑里面安装了一个GenyMotion模拟器,虽然速度不及真机,但是也算能解决大部分的需求. 安装完之后启动出现了以下错 ...

  6. ASP.NET 学习小记 -- “迷你”MVC实现(1)

    ASP.NET 由于采用了管道式设计,具有很好的扩展性.整个ASP.NET MVC应用框架就是通过扩展ASP.NET实现的.通过ASP.NET的管道设计,我们知道,ASP.NET的扩展点主要是体现在H ...

  7. c++二分答案 之 跳石头

    题目: 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之 ...

  8. JAVA程序性能分析及调优浅析

    1.性能分析本质 寻找系统的性能瓶颈(木桶理论/短板效应),并处理系统的性能瓶颈 2.性能分析主要指标负载.响应和服务器CPU\MEM等的使用率 3.性能分析主要工具 LoadRunner Visua ...

  9. 常用数据字典---bai

    --常用数据字典 -- system: normal; sysdba --查询所有的逻辑对象.所有. select count(1) from dba_objects; select * from d ...

  10. 转:Linux Shell编程入门

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来 ...