java Web项目中properties配置文件中的密码加密
2018-01-09 09:13
525 查看
get到一项新技能(properties文件中value替换),之前也想过properties中配置项暴露但没有落实,今天看到博客分享一下:
我们使用的项目经常是这个样子的:
这里会有一个致命的问题,如果有一个具备中间件服务器机器访问权限的人,看到了这个例如applicationContext.xml的文件,并且打开该文件,智商再低下的人也会知道数据库的用户名和密码是什么。这对于对安全有一定要求的行业是必须杜绝的,这个也是在一般技术面试中会问到的一个问题。那就让我们继续往下,解答这个问题吧!
首先,我们需要将配置文件抽取到property中来:
将上面的第一个代码修改为第二个代码,第一个类是负责抓取jdbc.properties中的属性并且填充到dataSource当中来,这样,我们就可以将所有的注意力都集中在jdbc.properties上了。
下面的问题是,如何将jdbc.properties变成一个看不明白的字符呢?我们只需要扩展PropertyPlaceholderConfigurer父类PropertyResourceConfigurer的解密方法convertProperty就可以了:
然后将上面完成的类替换配置文件中的PropertyPlaceholderConfigurer:
事实上,在我刚刚的Demo项目当中,里面的jdbc.properties里面的文件是如下内容的:
而实际上,真实的密码却是czw/czw,web程序运行的时候,显示如下的内容:
2013-8-31 13:26:12 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.18
2013-8-31 13:26:14 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
==================url:jdbc:oracle:thin:@127.0.0.1:1523:orcl
==================password:somePwdElseUnknowPassowrd
==================driverClassName:oracle.jdbc.driver.OracleDriver
==================userName:someOneElseUnknowUserName
2013-8-31 13:26:17 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'cardDemo'
2013-8-31 13:26:18 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2013-8-31 13:26:18 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
但是,在DATASOURCE里面获取到的内容,却是替换之后的正确的用户名和密码。这只是一个非常简单的例子,只是告诉我们一个解决数据库用户名和密码加密的一个渠道,比如,我们可以在PropertyPlaceholderConfigurer子类中写一些比较复杂的逻辑,比如根据jdbc.properties中配置的文件中进行各种手段的加密。在其中通过其他手段替换jdbc.propertes中的用户名和密码等等。
我们使用的项目经常是这个样子的:
1 | < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" |
2 | destroy-method = "close" |
3 | p:driverClassName = "oracle.jdbc.driver.OracleDriver" |
4 | p:url = "jdbc:oracle:thin:@127.0.0.1:1523:orcl" |
5 | p:username = "czw" |
6 | p:password = "czw" /> |
首先,我们需要将配置文件抽取到property中来:
01 | < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" |
02 | p:location = "classpath:jdbc.properties" |
03 | p:fileEncoding = "utf-8" |
04 | /> |
05 |
06 | < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" |
07 | destroy-method = "close" |
08 | p:driverClassName = "${driverClassName}" |
09 | p:url = "${url}" |
10 | p:username = "${userName}" |
11 | p:password = "${password}" /> |
下面的问题是,如何将jdbc.properties变成一个看不明白的字符呢?我们只需要扩展PropertyPlaceholderConfigurer父类PropertyResourceConfigurer的解密方法convertProperty就可以了:
01 | package com.cardDemo.commonUtil; |
02 |
03 | import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; |
04 |
05 | public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{ |
06 | @Override |
07 | protected String convertProperty(String propertyName, String propertyValue) { |
08 | System.out.println( "==================" +propertyName+ ":" +propertyValue); |
09 | if ( "userName" .equals(propertyName)){ |
10 | return "czw" ; |
11 | } |
12 | if ( "password" .equals(propertyName)){ |
13 | return "czw" ; |
14 | } |
15 | return propertyValue; |
16 | } |
17 | } |
1 | < bean class = "com.cardDemo.commonUtil.ConvertPwdPropertyConfigurer" |
2 | p:location = "classpath:jdbc.properties" |
3 | p:fileEncoding = "utf-8" |
4 | /> |
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1523:orcl
userName=someOneElseUnknowUserName
password=somePwdElseUnknowPassowrd
而实际上,真实的密码却是czw/czw,web程序运行的时候,显示如下的内容:
2013-8-31 13:26:12 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.18
2013-8-31 13:26:14 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
==================url:jdbc:oracle:thin:@127.0.0.1:1523:orcl
==================password:somePwdElseUnknowPassowrd
==================driverClassName:oracle.jdbc.driver.OracleDriver
==================userName:someOneElseUnknowUserName
2013-8-31 13:26:17 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'cardDemo'
2013-8-31 13:26:18 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2013-8-31 13:26:18 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
但是,在DATASOURCE里面获取到的内容,却是替换之后的正确的用户名和密码。这只是一个非常简单的例子,只是告诉我们一个解决数据库用户名和密码加密的一个渠道,比如,我们可以在PropertyPlaceholderConfigurer子类中写一些比较复杂的逻辑,比如根据jdbc.properties中配置的文件中进行各种手段的加密。在其中通过其他手段替换jdbc.propertes中的用户名和密码等等。
相关文章推荐
- java web项目中的properties文件中的属性值在启动后进行修改(数据库密码加密等)
- 加密Web项目中配置文件中的密码
- 加密Web项目中配置文件中的密码
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- 面试常见问题:如何加密Web项目中配置文件中的密码?
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- 如何加密Web项目中配置文件中的密码?
- 面试常见问题:如何加密Web项目中配置文件中的密码?
- java web项目读取配置文件(.properties)
- 读取项目中properties文件中的账号密码进行加密解密
- Web项目中对配置文件中的密码进行加密(数据库连接密码)
- 用一个随项目启动而初始化的servlet读取配置环境,参数的properties文件
- [★] 本地配置文件中存储密码 之 加密解密
- web项目实际部署中修改properties配置文件内容
- Openerp在配置文件中为数据库密码加密
- 加密jdbc配置文件中的用户名密码
- SpringBoot中加密包对配置文件中的密码进行加密
- MySQL连接配置文件密码加密及其在多种连接池上的应用
- eclipse java web项目数据库配置文件添加