How to Encrypt Spring Datasource Password Credentials

 Posted On  | Yashwant Chavan 

Its a bad programming practice to display credentials in the spring data-source configuration file. In this article we are going to use the Encrypted password with the help wrapper data-source class.

Lets follow the below steps to create sample Spring + hibernate application which use the Encrypted password to get the database connection. Spring framework provides the facility to extend the DriverManagerDataSource class and creates the custom class.

Useful to whom

This tutorial is useful for beginners and experience developers. It will helps you to learn step by step with the help of attached code.

Tools and Technologies

To execute sample Spring Hibernate program we use below technologies

  1. Maven 3.0.4
  2. JDK 1.6
  3. Hibernate
  4. Spring 2.5.6
  5. MySql 5.1

Maven dependancy pom.xml

Define Spring and Hibernate Dependencies in pom.xml, In this tutorial simply insert the record into employee table.

<project xmlns="" xmlns:xsi=""










Custom Spring Datasource (

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.

package com.technicalkeeda.dao.utility;


import org.springframework.jdbc.datasource.DriverManagerDataSource;

import sun.misc.BASE64Decoder;

public class EncryptedDataSource extends DriverManagerDataSource {

 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));
  } catch (IOException e) {
  return decode;


Spring Datasource Configuration (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. <property name="password"><value>cGFzc3dvcmQ=</value></property>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

 <!-- This is Database Configuration -->
 <bean id="dataSource" class="com.technicalkeeda.dao.utility.EncryptedDataSource">
  <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
  <property name="url"><value>jdbc:mysql://localhost:3306/technicalkeeda</value></property>
  <property name="username"><value>demo</value></property>
  <property name="password"><value>cGFzc3dvcmQ=</value></property>

 <bean id="employeeDao" class="com.technicalkeeda.dao.impl.EmployeeDaoImpl">
  <property name="sessionFactory" ref="hibernateSessionFactory" />

 <bean id="hibernateSessionFactory"
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
  <property name="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="">create</prop>


Hibernate Modal Class (

Create Employee model class with the help of hibernate annotations like @Entity, @Table(name = "trn_employee") and @Column(name = "employee_id")

package com.technicalkeeda.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "trn_employee")
public class Employee {

 @Column(name = "employee_id")
 private long employeeId;

 @Column(name = "first_name")
 private String firstName;

 @Column(name = "last_name")
 private String lastName;

 @Column(name = "email")
 private String email;

 public long getEmployeeId() {
  return employeeId;

 public void setEmployeeId(long employeeId) {
  this.employeeId = employeeId;

 public String getFirstName() {
  return firstName;

 public void setFirstName(String firstName) {
  this.firstName = firstName;

 public String getLastName() {
  return lastName;

 public void setLastName(String lastName) {
  this.lastName = lastName;

 public String getEmail() {
  return email;

 public void setEmail(String email) { = email;


Dao Interface (

Define a EmployeeDao interface with createEmployee() method. It is going to save the employee entity into trn_employee table.

package com.technicalkeeda.dao;

import com.technicalkeeda.entities.Employee;

public interface EmployeeDao {
 public void createEmployee(Employee employee);

Implemention class (

EmployeeDaoImpl is the implementation class. Which implements the createEmployee() method.

package com.technicalkeeda.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.technicalkeeda.dao.EmployeeDao;
import com.technicalkeeda.entities.Employee;

public class EmployeeDaoImpl implements EmployeeDao {

 private HibernateTemplate hibernateTemplate;

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.hibernateTemplate = new HibernateTemplate(sessionFactory);

 public void createEmployee(Employee employee) {


Test Class (

Build your maven project and execute the class, It is going to create trn_employee table in the database and insert one employee

package com.technicalkeeda.test;

import org.springframework.context.ApplicationContext;

import com.technicalkeeda.dao.EmployeeDao;
import com.technicalkeeda.entities.Employee;

public class App {
 public static void main(String[] args) {
  ApplicationContext appContext = new ClassPathXmlApplicationContext(

  Employee employee = new Employee();

  EmployeeDao employeeDao = (EmployeeDao) appContext.getBean("employeeDao");


When you execute the , output will look like this.

Hibernate: insert into trn_employee (email, first_name, last_name) values (?, ?, ?)

Promote your Blog, Website, or services on, My paypal id:

Payment will be accepted through paypal only.

© 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0300 seconds.