basicdatasource默认_如何在Apache中的BasicDataSource使用加密的密码?

本文探讨了如何在使用Spring和Apache DBCP的BasicDataSource时,将密码从属性文件加密并配置到XML配置文件中。通过扩展Ant的Copy任务和FilterSet,实现了在构建过程中自动加密密码,并创建了一个名为EncryptionAwareDataSource的类,该类继承自BasicDataSource并在设置密码时解密。这样,配置文件中的密码以加密形式存储,但在运行时会被正确解密使用。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

At present i am keeping the password [ unencrypted ] in a property file. This password get placed as is in the configuration xml using ant.

[ The configuration xml is for datasource, it is creating the object of dbcp.BasicDataSource ]

Now, is it possible that after the ant target the password is copied in encrypted form. Heard the Jasypt can do that! Till now i haven't tried this. But, the problem doesn't end here. BasicDataSource do not accept encrypted password. Is there any replacement for BasicDatasource.

FYI: I am using Spring, if that matters.

解决方案

Create a new task by extending existing task Copy( responsible for file-copy ). Create a new type by extending FilterSet ( responsible for filtering of tokens ).

see the code here:-

http://stackoverflow.com/questions/3657910/how-to-create-nested-element-for-ant-task

build.xml

blah-blah.properties

property1=value1

property2=value2

PASSWORD=^&YUII%%&*(

USERNAME=rjuyal

CONNECTION_URL=...

someotherproperty=value

configuration xml

class="com.xyz.datasource.EncryptionAwareDataSource"

destroy-method="close" autowire="byName">

com.ibm.db2.jcc.DB2Driver

@CONNECTION_URL@

@USERNAME@

@PASSWORD@

true

10

10

...

...

...

After the execution of the target the xml is copied with values from properties file. Password will be encrypted.

This will handle the encrypted password.

EncryptionAwareDataSource

public class EncryptionAwareDataSource extends BasicDataSource{

@Override

public synchronized void setPassword(String password) {

super.setPassword(Encryptor.getDecryptedValue( password ));

}

}

That' all ;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值