9

Trying to use encrypted database password to connect to database using spring auto configuration

I am using Spring auto configuration to connect to database. For that i added below properties in properties file:

spring.datasource.url=jdbc:oracle:thin:@ABCDE2D.com:1888:ABCDE2D1 spring.datasource.username=user spring.datasource.password=password spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver 

In my dao class, i have @Autowired NamedParameterJdbcTemplate and using it directly to get data from database.

Till here it is working fine.

Now i need to encrypt the password in properties file. For that i did the below:

  1. Added jasypt-spring-boot-starter in pom
  2. Added spring.datasource.password=ENC(NoIv2c+WQYF3LenN0tDYPA==) in properties file
  3. Added jasypt.encryptor.password=key in properties file

Now i am getting the below error:

Failed to bind properties under 'spring.datasource.password' to java.lang.String: Reason: Failed to bind properties under 'spring.datasource.password' to java.lang.String 
8
  • Can you please post the whole stacktrace Commented Jun 13, 2019 at 11:31
  • And another question. Why do you want to encrypt the password? It's worthless as you add the encryptor password to the properties file as well Commented Jun 13, 2019 at 11:32
  • have you added oracle dependency? please add your pom file.. Commented Jun 13, 2019 at 11:35
  • @SimonMartinelli : i have added in properties file for now. Once it will running file i will pass the key as environment variable. Commented Jun 13, 2019 at 12:09
  • And so follows the next stupid question: Why don't you just define the database password as environment variable? So can get rid of jasypt at all Commented Jun 13, 2019 at 12:13

4 Answers 4

16

I'm providing some basic guide as follows.

You need to add following two parameters in to property file in order application to work properly. This is assuming you are encrypting using default encryption algorithm. If you are useing some other, then make sure to change it accordingly.

jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator jasypt.encryptor.algorithm=PBEWithMD5AndDES

You can refer to more details https://nirmalbalasooriya.blogspot.com/2020/02/spring-boot-property-encryption-using.html

Sign up to request clarification or add additional context in comments.

1 Comment

How to make it working with default algorithm? stackoverflow.com/questions/77788336/…
3

In my case I was giving wrong jasypt.encryptor.password. For Example given below is properties I have set in my application.properties:

jasypt.encryptor.password=abc 

instead abc I gave jasypt as my secret key while encrypting the password so the encripted password is wrong. Then it throwing this error. Later I realised and found that the key is not correct. Then gave the right key i.e., abc.Then it worked for me. Silly mistake but it cost me 4 hours. Hope it will be useful for others.

Comments

-1

If you are using jasypt dependency, make sure that:

spring.datasource.password = Enc 

and

jasypt.encryptor.password = key 

where Enc is encrypted password and key is the key which you used to generate the encrypted password.

Comments

-1

I had the same issue and after troubleshooting I couldn't find

jasypt.encryptor.password 

in my application config yaml file but after some search on server level it was defined inside catalina.properties. If any user faces this kind of issue where they cannot find the encryptor password, then thoroughly check the properties files or environment variables defined on the server level.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.