java 文件打开密码_java – 保护属性文件中的密码

oEt2m.png

Jasypt提供了org.jasypt.properties.EncryptableProperties类,用于加载,管理和透明地解密.properties文件中的加密值,允许在同一文件中混合加密和未加密的值.

By using an org.jasypt.properties.EncryptableProperties object, an

application would be able to correctly read and use a .properties file

like this:

datasource.driver=com.mysql.jdbc.Driver

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

datasource.username=reportsUser

datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)

Note that

the database password is encrypted (in fact, any other property could

also be encrypted, be it related with database configuration or not).

How do we read this value? like this:

/*

* First, create (or ask some other component for) the adequate encryptor for

* decrypting the values in our .properties file.

*/

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();

encryptor.setPassword("jasypt"); // could be got from web, env variable...

/*

* Create our EncryptableProperties object and load it the usual way.

*/

Properties props = new EncryptableProperties(encryptor);

props.load(new FileInputStream("/path/to/my/configuration.properties"));

/*

* To get a non-encrypted value, we just get it with getProperty...

*/

String datasourceUsername = props.getProperty("datasource.username");

/*

* ...and to get an encrypted value, we do exactly the same. Decryption will

* be transparently performed behind the scenes.

*/

String datasourcePassword = props.getProperty("datasource.password");

// From now on, datasourcePassword equals "reports_passwd"...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值