我写了一个解密类读取properties里面的数据库帐号和密码然后解密,为什么在需要操作数据库的时候,拿的还是我加密的密文,而没经过解密的方法了?有什么办法可以让spring去拿的是解密后的帐号密码,而不是密文
解决方案 »
- Hibernate Dao访问问题
- 轻量级Java EE企业应用实战(第3版):Struts 2+Spring 3+Hibernate整合开发 这本书怎么样??
- myeclise6.6调用C++的问题?
- 一个非常奇怪的问题,关闭子页面刷新父页面,父页面包含了iframe页面
- javaScript如何迭代action中的集合?
- Spring+Hibernate的问题
- HIBERNATE如下SQL如何配置关联,高分
- 机种/OS/DB/语言 这个机种是什么意思?
- TapeStry+Spring部署的问题
- 想要一个servlet+jsp的源码:如何将ejb中返回的collection类型先显示了前10个,然后按下一页,继续显示剩下的10个......
- 如何实现新邮件自动提醒功能
- ie不能下载,有遇到过不?
然后覆写以下方法,应该可以解决你的问题
protected String convertPropertyValue(String originalValue)
{
return originalValue;
}
Convert the given property value from the properties source to the value that should be applied.
The default implementation simply returns the original value. Can be overridden in subclasses, for example to detect encrypted values and decrypt them accordingly.
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="" class="com.zhangyz.www.spring.MinePropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/zhangyz/www/spring/jdbc.properties</value>
</property>
</bean>
<bean name="test" class="com.zhangyz.www.spring.Test">
<property name="userName" value="${test.userName}">
</property>
<property name="password" value="${test.password}">
</property>
</bean>
</beans>Java类package com.zhangyz.www.spring;import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {
private String userName;
private String password;
public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} private static Logger logger = Logger.getLogger(Test.class.getName()); /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"/com/zhangyz/www/spring/application.xml");
Test test=(Test) applicationContext.getBean("test");
logger.error(test.getUserName());
logger.error(test.getPassword());
}
}
package com.zhangyz.www.spring;import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class MinePropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Logger logger = Logger
.getLogger(MinePropertyPlaceholderConfigurer.class.getName()); protected String convertPropertyValue(String originalValue) {
return originalValue+"哥哥来解密!";
}}properties文件:test.userName=121313132132
test.password=pppppppppppp
2011-11-24 13:26:21,750 ERROR [main - (Test.java:36) - com.zhangyz.www.spring.Test] 121313132132哥哥来解密!
2011-11-24 13:26:21,750 ERROR [main - (Test.java:37) - com.zhangyz.www.spring.Test] pppppppppppp哥哥来解密!
这是输出结果,你在那个地方处理下加密解密不就可以了
properties文件:test.userName=121313132132
test.password=after-encrypt:ppppppppppppjava文件package com.zhangyz.www.spring;import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class MinePropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private String encrypt="after-encrypt:";
private static Logger logger = Logger
.getLogger(MinePropertyPlaceholderConfigurer.class.getName()); protected String convertPropertyValue(String originalValue) {
if(originalValue!=null&&originalValue.length()>encrypt.length()&&originalValue.indexOf(encrypt)>=0){
return originalValue.substring(encrypt.length());
}else{
return originalValue;
}
}}
[Quote]
你直接这样改spring的配置你觉得spring可以识别?
使用:PropertiesUtils.getAll();package com.commons;import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;public class PropertiesUtils {
private static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
private static final String DEFAULT_RESOURCE_PATTERN = "classpath:*-tag.properties";
private static final Map<Object, Object> all = new HashMap<Object, Object>(); public static Map<Object, Object> getAll() {
return all;
} static {
try {
Resource[] resources = resourcePatternResolver
.getResources(DEFAULT_RESOURCE_PATTERN);
if (resources != null) {
for (Resource r : resources) {
Reader in = new InputStreamReader(r.getInputStream(),
"UTF-8");
Properties p = new Properties();
p.load(in);
all.putAll(p);
in.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}}