admin管理员组文章数量:1639833
Web项目的如配置文件applicationContext.xml文件中,如数据库连接的用户密码信息的加密。
我们平时直接写的项目配置是这样的:
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="net.sourceforge.jtds.jdbc.Driver"
p:url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=demo1"
p:username="abc"
p:password="12345678" />
对于用户信息,一般我们将其提取到property文件中
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"
p:fileEncoding="utf-8"
/>
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${userName}"
p:password="${password}" />
为了杜绝一个具备中间件服务器机器访问权限的人,看到了这个例如applicationContext.xml的文件,我们仅仅把它写入property文件还不够,因为这样在web运行后在
后台还是会直接显露出用户名密码。
故其中最关键的点就放在了上面抓取jdbc.properties类中了。
下面的问题是,如何将jdbc.properties变成一个看不明白的字符呢?我们只需要扩展PropertyPlaceholderConfigurer父类PropertyResourceConfigurer的解密方法convertProperty就可以了:
package com.hfmon;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{
@Override
protected String convertProperty(String propertyName,String propertyValue)
{
System.out.println("=================="+propertyName+":"+propertyValue);
if("dataSource.username".equals(propertyName))
{
return "abc";
}
if("dataSource.password".equals(propertyName))
{
return "1234567";
}
return propertyValue;
}
}
然后将上面完成的类替换配置文件中的PropertyPlaceholderConfigurer:
<bean id="propertyConfig"
class="com.hfmon.ConvertPwdPropertyConfigurer">
<property name="locations"><!-- locations应为类中的属性 -->
<value>classpath:jdbc.properties</value>
</property>
</bean>
而在property文件中可以对其中的想要 加密 的信息改写:
username=someOneElseUnkonwUserName
password=someOneElseUnkonwUserName
driverClassName=net.sourceforge.jtds.jdbc.Driver
url=jdbc\:jtds\:sqlserver\://localhost\:1433;DatabaseName\=demo
其实其中原理还是,通过使用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息.properties
而通过重写PropertyPlaceholderConfigurer的子类方法convertProperty()来实现。
版权声明:本文标题:Web项目中对配置文件中的密码进行加密(数据库连接密码) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729296345a1194768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论