Encrypt Spring Datasource Password

Encrypt Spring Datasource Password

Password encryption is first mandatory field that should be encrypted in application and it does not matter what kind of framework you are using. In this tutorial I will show you how to encrypt password in Spring based application. You probably may have seen many documentation and API which can be use to encrypt password in Spring properties file. Here I will show you simple technique to encrypt password without doing much coding and easy to understand. Although password encryption part is simple but to understand better I will create small Spring MVC web application and will make one call to database to verify if everything works.

Note: To keep this application simple and easy to understand I will use Base64 to encrypt the password. It’s not advisable to use Base64 technique to encrypt the password. I have written many encryption technique for encryption as you could see in these tutorial.

Tools needed:

  • Eclipse
  • MySQL
  • JDK 1.8
  • Maven 3.2


  • Create sample table in MySQL database and insert some dummy data:

  • Insert data:

  • Create maven project name SpringPasswordEncryption below is final project structure:

Encrypt Spring Datasource Password

  • web.xml:

  • pom.xml:

  • datasource.properties where we will store the password as you could see below password has been encrypted and displayed here:

Important: Configuration: In dispatcher-servlet.xml as you see, I have created datasource bean which is using class: com.javahonk.dao.CustomDriverManagerDataSource this class extends DriverManagerDataSource manager class. You will see full details of this class below but this what you need to do in your application to encrypt and decrypt the password.

  • dispatcher-servlet.xml:

  • CustomDriverManagerDataSource.java:

  • SpringMVCController.java:

  • IPersonDAO.java:

  • PersonDAOImpl.java:

  • These are important file shown here other are included in bottom to download. You could run this application on any server if everything setup correctly. Here I will be using Tomcat server to run this application on MySQL database server. To run this application right click project –> Run As –> Run On Server (Before doing this you will have to download and configure Tomcat server in Eclipse if you have not done yet please follow this tutorial to configure Tomcat in Eclipse). You will see below first page:

Encrypt Spring Datasource Password

  • As you remember we have create table in MySQL database and to fetch the data from database we also create one method in SpringMVCController.java its mapping URL is “/databasetest”. Let’s use this in URL to test if we get data from database or not. If everything goes well you will see below output:

Encrypt Spring Datasource Password

download Download Project: SpringPasswordEncryption

  • For more information on please visit Spring site here

Leave a Reply

Your email address will not be published. Required fields are marked *