在Spring xml文件中隐藏数据源密码 [英] Hide datasource password in spring xml file
问题描述
有没有一种方法可以在xml spring配置文件中隐藏/加密密码? 我读到DataSource的自定义"子类是可能的,但是解决方案将密钥与纯文本保存在相同的配置文件中...因此有点没用.
there is a way to hide/encrypt password in xml spring config file? I read that is possible with a "custom" subclass of DataSource, but the solutions keep key in same config file as plain text...so is a bit useless.
有一种使用KeyStore的方法吗? 例如,从密钥库中读取值.
There is a way to use KeyStore for this? For example read the value from a keystore.
谢谢.
推荐答案
是的,您可以这样做.您将必须围绕数据源类创建一个包装器bean.这是我以前做过的一个例子.希望这会有所帮助!
Yes, you can do that. You will have to create a wrapper bean around the data source class. Here is an example of how I have done it before. Hope this helps!
<beans>
<bean id="someDao" class="com.dao.SomeDAOImpl">
<property name="datasource">
<ref local="secureDataSource"/>
</property>
</bean>
<bean id="secureDataSource" class="com.ds.SecureDataSource">
<property name="driverClassName">
<value><your driver></value>
</property>
<property name="url">
<value><your url></value>
</property>
<property name="username">
<value><your user id></value>
</property>
<property name="password">
<value><encrypted_pwd></value>
</property>
</bean>
</beans>
然后在SecureDataSource类中,您将需要解密密码.
Then inside the SecureDataSource class you will need to decrypt the password.
import java.sql.Connection;
import java.sql.SQLException;
public class SecureDataSource extends DriverManagerDataSource{
private String url;
private String username;
private String password;
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
protected Connection getConnectionFromDriverManager() throws SQLException {
String decryptedPassword = null;
//decrypt the password here
return getConnectionFromDriverManager(url,username,decryptedPassword);
}
}
这篇关于在Spring xml文件中隐藏数据源密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!