java用户名密码的加密_JAVA:spring数据源中使用加密的用户名和密码

步骤1、jdbc.properties文件中配置用户名、密码等

jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

jdbc.username=xxxx

jdbc.password=yyyy

步骤2、spring.xml中配置资源文件

方法一,使用明文用户名和密码时,直接配置文件位置:

spring.xml

方法二,spring中配置并添加一个类继承PropertyPlaceholderConfigurer实现使用加密字符串,使用时解密

spring.xml

classpath:jdbc.properties

jdbc.username

jdbc.password

jdbc.url

EncryptPropertyPlaceholderConfigurer.java

package xxx.xxx;

import java.util.Set;

import org.apache.commons.lang3.ObjectUtils;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**

* 继承自spring的PropertyPlaceholderConfigurer来扩展

*

*/

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

private Set encryptedProps;

/** 需要解密的字段 */

private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };

protected String convertProperty(String propertyName, String propertyValue) {

if (isEncryptProp(propertyName)) {

// 这里进行解密(根据实际内容改成使用的解密方法)

return Util.xxxx(propertyValue);

} else {

return propertyValue;

}

}

/**

* 判断属性是否需要解密

*

* @param propertyName

* @return

*/

@SuppressWarnings("deprecation")

private boolean isEncryptProp(String propertyName) {

for (String encryptpropertyName : encryptPropNames) {

if (ObjectUtils.equals(encryptpropertyName, propertyName)) {

return true;

}

}

return false;

}

public Set getEncryptedProps() {

return encryptedProps;

}

public void setEncryptedProps(Set encryptedProps) {

this.encryptedProps = encryptedProps;

}

}

标签:jdbc,return,String,数据源,propertyName,spring,JAVA,encryptedProps

来源: https://www.cnblogs.com/leonlipfsj/p/12256025.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值