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配置 |
Spring读取加密属性文件处理的更多相关文章
- Spring读取加密属性文件处理--待整理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...
- Java学习笔记——JDBC读取properties属性文件
Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...
- spring读取加密配置信息
描述&背景Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存 ...
- Spring读取外部属性-properties
概述 在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取.比如数据源信息配置,Java固定属性位置等.读取的方式一般由三种: ...
- spring 使用外部属性文件
一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...
- 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)
一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...
- Spring中使用属性文件properties的两种方式
实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...
- Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...
- Spring使用外部属性文件
一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...
随机推荐
- Linux 系统结构详解
Linux 系统结构详解 Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统 ...
- Linux之在CentOS上一次艰难的木马查杀过程
今天朋友说他一台要准备上线的生产服务器被挂马,特征ps命令找不到进程,top能看到负载最高的一个程序是一个随机的10位字母的东西,kill掉之后自动再次出现一个随机10位字母的进程. 我让他关闭这个机 ...
- Understanding Manycore Scalability of File Systems
多核场景下,不同文件系统,文件操作的性能评估.
- CentOS添加RPMforge软件源
1.查看CentOS版本 cat /etc/redhat-release 2.查看系统位数 uname -i 3.下载rpm包 wget "rpm地址" CentOS 6: i68 ...
- centos6.5 mysql配置整理
安装 // 安装mysql yum -y install mysql-server //设置开机启动 chkconfig mysqld on //启动MySql服务 service mysqld st ...
- Python Mixin混入的使用方法
DEMO # encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Base(): def f1(self): print 'I am f1 i ...
- This transaction has been rolled back, rather than only the current.
今天上午,收到运维组同事反映某应用系统的其中一个功能报错,不是偶然性事件,每个使用该功能的用户都报错.报错内容为:This transaction has been rolled back, rath ...
- Extjs 更新数据集Ext.PagingToolbar的start参数重置的处理
问题:当翻页后,比如当前是第二页,start参数此时是5(初始为0),当切换左侧分类时,我们期望的是从所选分类下明细记录的第一条开始显示,结果发现不是这样,依然是从新数据的第二页开始显示,就是说ext ...
- iOS崩溃日志分析-b
1名词解释 1.1. UUID 一个字符串,在iOS上每个可执行文件或库文件都包含至少一个UUID,目的是为了唯一识别这个文件. 1.2. dwarfdump 苹果提供的命令行工具,其中一些功能就是查 ...
- android开发之---文字居中---android中去掉标题栏
1. 让textView里面的内容水平居中 : android:gravity="center_horizontal" 2. 让textView控件在它的父布局里水平居中 ...