Reputation: 9775
Here is my configuration file which i use to create DAO object.
<?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>
Normally we can create database object as follows
ApplicationContext context =
new ClassPathXmlApplicationContext("Parent.xml");
FIBillingDAOImpl dao =
(FIBillingDAOImpl)context.getBean("fiBillJDBCTemplate");
But this wont work as the password stored in xml file is encrypted.
How can an DAO object be made in such a way that password will be decrypted.
Upvotes: 1
Views: 7209
Reputation: 4096
The Class you use here (org.springframework.jdbc.datasource.DriverManagerDataSource
) can't be changed.
I suggest you either :
Override this class, and override the setPassword method in order to decrypt the password value.
Use a custom Property placeholder that does encryption (something like this)
Upvotes: 1
Reputation: 15718
You can extend DriverManagerDataSource
to handle the decryption logic. You need to add the logic to decode password and return it from decode
method E.g.
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;
}
}
After that in your spring configuration file use this class as datasource
<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>
Upvotes: 5