java怎么输入basic密码_java – 如何在apache BasicDataSource中使用加密密码?

与Spring有更好的方式:使用

PropertyPlaceholderConfigurer类.

classpath:com/foo/jdbc.properties

class="org.apache.commons.dbcp.BasicDataSource">

当您在属性占位符中指定PropertiesPersister的子类时,Spring会加载jdbc.properties并使用该类解密该文件.也许像:

public class MyPropertyPersister extends DefaultPropertiesPersister

{

// ... initializing stuff...

public void load(Properties props, InputStream is) throws IOException

{

Cipher decrypter = getCipher();

InputStream cis = new CipherInputStream(is, decrypter);

super.load(props, cis);

}

public void load(Properties props, Reader reader) throws IOException

{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

IOUtils.copy(reader, baos);

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

Cipher decrypter = getCipher();

InputStream cis = new CipherInputStream(bais, decrypter);

InputStreamReader realReader = new InputStreamReader(cis);

super.load(props, realReader);

}

public void loadFromXml(Properties props, InputStream is) throws IOException

{

Cipher decrypter = getCipher();

InputStream cis = new CipherInputStream(is, decrypter);

super.loadFromXml(props, cis);

}

private Cipher getCipher()

{

// return a Cipher to read the encrypted properties file

...

}

...

}

希望它有帮助.

编辑

如果您使用Jasypt,则不需要定义任何PropertiesPersister.从Jasypt documentation:

Jasypt provides an implementation of these configuration-related Spring classes which can read .properties files with encrypted values (like the ones managed by the EncryptableProperties class) and handle them transparently to the rest of the Spring application beans.

有了这个,你可以像这样定义jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/reportsdb

jdbc.username=reportsUser

jdbc.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)

并且Spring配置可能是这样的

/WEB-INF/classes/jdbc.properties

class="org.apache.commons.dbcp.BasicDataSource">

这样,您可以在启动应用程序时将密码解密为环境变量中的隐藏属性,并在以后取消设置.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值