Encrypt DBCP Datasource Password Spring

Encrypt DBCP Datasource Password Spring

Password encryption is mandatory for any application as you saw in last tutorial how to encrypt password if you are using Spring datasource. In this tutorial I will show how to encrypt password if you are using DBCP basic datasource to make connection to the database. As all steps are same here, only you will have to extends BasicDataSource class from DBCP.

  • Sample Spring MVC project:

Encrypt DBCP Datasource Password Spring

  • dispatcher-servlet.xml where you are making connection to the database:

As you see above in place of extending <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”> I have written new class CustomBasicDataSource.java which extends BasicDataSource as below:

  • CustomBasicDataSource.java:

You can use any encryption algorithm here we have used Base64 to encrypt password as sample class is below for encryption:

  • Base64PasswordEncrypter.java:

Please encrypt you password using this class and include in your properties file as below:

  • database.properties (Don’t forget to change with your database properties)

  • That’s it. You could download complete working project below. For information on DBCP please visit Apache DBCP official documentation here

download Download Project:  ConnectionPoolingDBCPSpring

Leave a Reply

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