Spring : Encrypt Spring Datasource Password Credentials
Encrypt Spring Datasource Password Credentials
(1) EncryptedDataSource.java I have written a wrapper class EncryptedDataSource for org.springframework.jdbc. datasource. DriverManagerDataSource and overridden getPassword() method. EncryptedDataSource class is injected in spring datasource configuration xml file. Which calls the overriden getPassword() method of data-source at the time of database connection. getPassword() method decode the encrypted password into actual password. To decrypt or encrypt password we used "BASE64Decoder" algorithm.
import java.io.IOException;
import org.springframework.jdbc. datasource. DriverManagerDataSource;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class EncryptedDataSource extends DriverManagerDataSource{
@Override
public String getPassword() {
String password = super.getPassword();
return decode(password);
}
/***
* Decode Password
*/
private String decode(String decode) {
BASE64Decoder decoder = new BASE64Decoder();
try {
decode = new String(decoder.decodeBuffer( decode));
System.out.println("Password Decrypted successfully");
} catch (IOException e) {
e.printStackTrace();
}
return decode;
}
/**
* @param args
*/
public static void main(String[] args) {
String password="atlas";
// TODO Auto-generated method stub
System.out.println("Password:" +password);
BASE64Encoder encoder = new BASE64Encoder();
String encryptedPassword=encoder. encode(password.getBytes());
System.out.println(" encryptedPassword:"+ encryptedPassword);
}
}
(2) spring-configuration.xml Bean datasource is define with custom EncryptedDataSource class , rather than DriverManagerDataSource class. Here I have passed the encrypted password to the spring data-source.
Change datasource as shown below
<!--<bean id="datasource" class="org.apache.commons.dbcp .BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver. OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@hostname: port:schemaname"/>
<property name="username" value="${dbusername}"/>
<property name="password" value="${dbpassword}"/>
</bean>
-->
<bean id="datasource" class="EncryptedDataSource">
<property name="driverClassName" value="oracle.jdbc.driver. OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@hostname: port:schemaname"/>
<property name="username" value="${dbusername}"/>
<property name="password" value="${dbpasswordencrypted}" />
</bean>
(1) EncryptedDataSource.java I have written a wrapper class EncryptedDataSource for org.springframework.jdbc.
import java.io.IOException;
import org.springframework.jdbc.
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class EncryptedDataSource extends DriverManagerDataSource{
@Override
public String getPassword() {
String password = super.getPassword();
return decode(password);
}
/***
* Decode Password
*/
private String decode(String decode) {
BASE64Decoder decoder = new BASE64Decoder();
try {
decode = new String(decoder.decodeBuffer(
System.out.println("Password Decrypted successfully");
} catch (IOException e) {
e.printStackTrace();
}
return decode;
}
/**
* @param args
*/
public static void main(String[] args) {
String password="atlas";
// TODO Auto-generated method stub
System.out.println("Password:"
BASE64Encoder encoder = new BASE64Encoder();
String encryptedPassword=encoder.
System.out.println("
}
}
(2) spring-configuration.xml Bean datasource is define with custom EncryptedDataSource class , rather than DriverManagerDataSource class. Here I have passed the encrypted password to the spring data-source.
Change datasource as shown below
<!--<bean id="datasource" class="org.apache.commons.dbcp
<property name="driverClassName" value="oracle.jdbc.driver.
<property name="url" value="jdbc:oracle:thin:@hostname:
<property name="username" value="${dbusername}"/>
<property name="password" value="${dbpassword}"/>
</bean>
-->
<bean id="datasource" class="EncryptedDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.
<property name="url" value="jdbc:oracle:thin:@hostname:
<property name="username" value="${dbusername}"/>
<property name="password" value="${dbpasswordencrypted}"
</bean>
Please leave your comments and suggestions below...
Comments
Post a Comment