Druid连接池自定义数据库密码加解密怎么实现

 我来答
地瓜说机
推荐于2016-01-22 · TA获得超过2.9万个赞
知道大有可为答主
回答量:2.3万
采纳率:91%
帮助的人:1.1亿
展开全部
@wenshao 你好,想跟你请教个问题:我实现一个DruidPasswordCallback子类,并配置到dataSource的property中:
?

1
2
3

<property name="passwordCallback" >
<bean class="utils.DBPasswordCallback" />
</property>

DBPasswordCallback类实现:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

public class DBPasswordCallback extends DruidPasswordCallback {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;

@Override
public void setProperties(Properties properties)
{
super.setProperties(properties);
String pwd = properties.getProperty("password");
if (!StringUtil.isTrimEmpty(pwd)) {
try {
setPassword(EncryptUtil.decrypt2AES(pwd).toCharArray());
} catch (Exception e) {
setPassword(pwd.toCharArray());
}
}
}
}

可是无法解密。 调试时发现properties是空的({}),取不到jdbc配置的原始密码,所以后面的操作也就没有意义了。 大家知道正确的做法吗?
Druid版本1.0.2

单车架构师
发帖于 2年前
3回/634阅
标签: Druid

举报
| 分享到

0收藏(1)
按票数排序 显示最新答案 共有3个答案 (最后回答: 2年前 )
0
单车架构师2年前
有人知道吗?
评论(0)| 引用此答案| 举报 (2014-02-22 12:57)
0
兮风古道2年前
可以扩展Spring的PropertyPlaceholderConfigurer,摘录别人一段代码你参考

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
/**
* 重写父类方法,解密指定属性名对应的属性值
*/
@Override
protected String convertProperty(String propertyName,String propertyValue){
if(isEncryptPropertyVal(propertyName)){
return DesUtils.getDecryptString(propertyValue);//调用解密方法
}else{
return propertyValue;
}
}
/**
* 判断属性值是否需要解密,这里我约定需要解密的属性名用encrypt开头
* @param propertyName
* @return
*/
private boolean isEncryptPropertyVal(String propertyName){
if(propertyName.startsWith("encrypt")){
return true;
}else{
return false;
}
}
}

评论(0)| 引用此答案| 举报 (2014-02-22 13:37)
0
单车架构师2年前
引用来自“兮风古道”的答案
可以扩展Spring的PropertyPlaceholderConfigurer,摘录别人一段代码你参考

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
/**
* 重写父类方法,解密指定属性名对应的属性值
*/
@Override
protected String convertProperty(String propertyName,String propertyValue){
if(isEncryptPropertyVal(propertyName)){
return DesUtils.getDecryptString(propertyValue);//调用解密方法
}else{
return propertyValue;
}
}
/**
* 判断属性值是否需要解密,这里我约定需要解密的属性名用encrypt开头
* @param propertyName
* @return
*/
private boolean isEncryptPropertyVal(String propertyName){
if(propertyName.startsWith("encrypt")){
return true;
}else{
return false;
}
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式