数据库用户名和密码加密

数据库用户名和密码加密

配置文件

db-context.xml页面先配置上这个类EncryptPropertyPlaceholderConfigurer类是需要新建的


<bean  class="test.startup.EncryptPropertyPlaceholderConfigurer" p:locations="classpath*:*.properties"></bean>

没有这个p标签的也需要添加上


 xmlns:p="http://www.springframework.org/schema/p

EncryptPropertyPlaceholderConfigurer类的代码



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

 * 
 * @Description 数据库加密
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{

	// 属性需与配置文件的KEY保持一致   加密名单
	private String encryptPropName ="灵活配置需要加密的字段";
	//加密状态是否已加密 0-否 1-是
	private String isencoder = "1";
 
	/**
	 * @param propertyName 属性名称
	 * @param propertyValue 属性值
	 */
	@Override
	protected String convertProperty(String propertyName, String propertyValue) {		
		//加密状态是否已加密 0-否 1-是
		if(("1").equals(isencoder)){
			// 如果在加密属性名单中发现该属性
			if (isEncryptProp(propertyName)) {			
				//解密或者其他方式
				String decryptValue = DES.decoder(propertyValue);			
				
				return decryptValue;
			} else {
				return propertyValue;
			}
		}else{
			return propertyValue;
		}
	}
 
	/**
	 * 
	 * @Description 判断是否在加密的名单里
	 * @param propertyName
	 */
	private boolean isEncryptProp(String propertyName) {
		String[] encryptPropNames = encryptPropName.split(",");
		for (String encryptName : encryptPropNames) {
			if (encryptName.equals(propertyName)) {
				return true;
			}
		}
		return false;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值