nixvega
nixvega

Reputation: 23

Encrypting/Decrypting database password programatically in SpringBoot

First time posting for help here! As you know, to add a data source(in my case MySql), I need to specify the username and password for the data source with the lines below (in my case it is demopassword):

spring.datasource.username=root
spring.datasource.password=demopassword

I have a project where the client requested me to encrypt and decrypt the password in the program like it is included in HibernateUtil class where SessionManager is created. I have tried using jasypt, succesfuly encrypting the password but I have to keep the secret key required by jasypt in the application properties as well. Like the example below:

spring.datasource.username=root
spring.datasource.password=ENC(B8xLWaCTFe38G5gbICDxyg==)
jasypt.encryptor.password=test

Is there any way I can achieve this? I do not neccesarily need to use application.properties. I am required to enter, encrypt and decrypt the database password programmatically. Thanks in advance!

Upvotes: 0

Views: 1236

Answers (1)

Lee Greiner
Lee Greiner

Reputation: 1082

You can use Spring Cloud Config encryption to do this. After encrypting your data with the Spring Boot CLI take the resulting encrypted string and preface it with {cipher} like '{cipher}lakjd98iyuolh98ytohaglj098u0u0[n='. You will need to add the -DENCRYPT_KEY=key when running your application or have it as an environment variable. The key value is the value used to encrypt the data using the Spring Boot CLI.

Upvotes: 0

Related Questions