引言: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. 关于$.fn

    今天看一篇文章,里面的一段代码出现了$.fn,第一次见到这样的写法,于是跑去问度娘...代码如下: $.fn.scrollUnique = function() { return $(this).ea ...

  2. csdn博客刷粉代码

    原理是当有访客访问博客时,执行js实现自动加粉丝,达到刷粉的目的. <script src="http://code.jquery.com/jquery-1.4.1.min.js&qu ...

  3. Linux时间设置

    Linux时间分为系统时间和硬件时间. 查看系统时间:date 将系统时间写入硬件:hwclock --systohc 查看硬件时间:hwclock --show 将硬件时间写入系统:hwclock ...

  4. jenkins 重新设置 管理员密码

    由于服务器瘫痪,修好之后经常不上,就把jenkins的管理密码忘掉了. 查阅了网上所有方案之后发现没有一个 能正确修改密码的,特此列出下列网上的方法 第一.设成无需密码验证的(网上有教程,不过并不能修 ...

  5. Pycharm使用技巧

    1.代码配色,即主题 pycharm自带的配色方案都很难看,网上的配色方案又很难看,所以根据其他ide的Monokai配色方案,自己定义了一个. pycharm Monokai主题下载:http:// ...

  6. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  7. Windows常用CMD命令

    远程桌面:mstsc 记事本:notepad        写字板:write 计算器:calc IIS重启:iisreset 60秒倒计时关机命令:tsshutdn     15秒关机:rononc ...

  8. CoreBluetooth - 中心模式

    BLE中心模式流程-coding BLE中心模式流程 - 1.建立中心角色 - 2.扫描外设(Discover Peripheral) - 3.连接外设(Connect Peripheral) - 4 ...

  9. YARN加载本地库抛出Unable to load native-hadoop library解决办法

    YARN加载本地库抛出Unable to load native-hadoop library解决办法 用官方的Hadoop 2.1.0-beta安装后,每次hadoop命令进去都会抛出这样一个War ...

  10. pgrep 查询进程的工具

    pgrep 1:简介 pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行.在服务器的配置和管理中,这个工具常被应用,简单明了: 1:用法 #pgrep 参数选项 程序名 常用 ...