在hibernate.cfg.xml中,用户和密码是明文存放的,存放某些安全问题,可以重写dataSource类来实现对配置信息加密的解密方法
<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName"
value="${jdbc.driverClassName}" />
<property
name="url"
value="${jdbc.url}" />
<property
name="username"
value="${jdbc.username}" />
<property
name="password"
value="${jdbc.password}" />
</bean>
比如这个配置中我们需要重写org.springframework.jdbc.datasource.DriverManagerDataSource
这个类继承于AbstractDriverBasedDataSource
可以写个类继承AbstractDriverBasedDataSource,重写里面的方法
比如用户、密码照着DriverManagerDataSource改掉相应的方法
/**
* Create a new DriverManagerDataSource with the given standard
* DriverManager parameters.
* @param url the JDBC URL to use for accessing the DriverManager
* @param username the JDBC username to use for accessing the DriverManager
* @param password the JDBC password to use for accessing the DriverManager
* @see java.sql.DriverManager#getConnection(String, String, String)
*/
public DriverManagerDataSource(String url, String username, String password) {
setUrl(url);
setUsername(username);
setPassword(password);
}
/**
* Create a new DriverManagerDataSource with the given JDBC URL,
* not specifying a username or password for JDBC access.
* @param url the JDBC URL to use for accessing the DriverManager
* @param conProps JDBC connection properties
* @see java.sql.DriverManager#getConnection(String)
*/
public DriverManagerDataSource(String url, Properties conProps) {
setUrl(url);
setConnectionProperties(conProps);
}
修改后
/**
* Create a new DriverManagerDataSource with the given standard
* DriverManager parameters.
* @param url the JDBC URL to use for accessing the DriverManager
* @param username the JDBC username to use for accessing the DriverManager
* @param password the JDBC password to use for accessing the DriverManager
* @see java.sql.DriverManager#getConnection(String, String, String)
*/
public DriverManagerDataSource(String url, String username, String password) {
setUrl(url);
setUsername(DesEncrypter.getInstance().decrypt(username));
setPassword(DesEncrypter.getInstance().decrypt(password));
}
/**
* Create a new DriverManagerDataSource with the given JDBC URL,
* not specifying a username or password for JDBC access.
* @param url the JDBC URL to use for accessing the DriverManager
* @param conProps JDBC connection properties
* @see java.sql.DriverManager#getConnection(String)
*/
public DriverManagerDataSource(String url, Properties conProps) {
setUrl(url);
if(conProps.containsKey("user")){
conProps.setProperty("user", DesEncrypter.getInstance().decrypt(conProps.getProperty("user")));
}
if(conProps.containsKey("password")){
conProps.setProperty("password", DesEncrypter.getInstance().decrypt(conProps.getProperty("password")));
}
setConnectionProperties(conProps);
}
最后把dataSource改为重写的类
<bean
id="dataSource"
class="com.ht.platform.datasource.DriverManagerDataSource">
PS:如果使用第三方的连接器,CustomDriverManagerConnectionProvider则需要继承于相应的连接器,如C3P0ConnectionProvider
本文介绍了如何解决在hibernate.cfg.xml中明文存放数据库用户和密码的安全问题。通过重写Spring的DriverManagerDataSource类,实现配置信息的加密解密方法,确保敏感数据的安全。具体操作包括创建一个继承自AbstractDriverBasedDataSource的类,覆盖相关方法以处理加密的用户和密码,然后将应用的dataSource替换为这个自定义类。如果使用第三方连接池,如C3P0,需要继承相应的ConnectionProvider并进行相应改造。
1468

被折叠的 条评论
为什么被折叠?



