Abh*_*ngh 1 java spring-jdbc password-encryption
这是我用来创建 DAO 对象的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@XXX.XX.XX.XXX:XXX:XXX"/>
<property name="username" value="encryptedUsername"/>
<property name="password" value="decrytedPassword"/>
</bean>
<bean id="fiBillJDBCTemplate" class="com.tfl.fi.billing.dao.FIBillingDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
通常我们可以如下创建数据库对象
ApplicationContext context =
new ClassPathXmlApplicationContext("Parent.xml");
FIBillingDAOImpl dao =
(FIBillingDAOImpl)context.getBean("fiBillJDBCTemplate");
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为存储在 xml 文件中的密码是加密的。
如何制作 DAO 对象以解密密码。
您可以扩展DriverManagerDataSource
来处理解密逻辑。您需要添加逻辑来解码密码并从decode
方法返回它,例如
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
public class EncryptedDriverManagerDataSource extends DriverManagerDataSource {
@Override
public String getPassword() {
String password = super.getPassword();
return decode(password);
}
/**
* You can implement you own decoding method.
*/
private String decode(String decode) {
//HERE
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
之后在你的 spring 配置文件中使用此类作为数据源
<bean id="dataSource" class="EncryptedDriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@XXX.XX.XX.XXX:XXX:XXX"/>
<property name="username" value="foo"/>
<property name="password" value="encryptedPassword"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6985 次 |
最近记录: |