We should encrypt some sensitive properties like password in real time projects to avoid hacking.
Here we are using jasypt-spring-boot dependency with spring boot project to encrypt properties and use those properties in code.
Below are the dependencies for different build tools:
Maven:
1 2 3 4 5 |
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.7</version> </dependency> |
Gradle:
1 |
compile('com.github.ulisesbocchio:jasypt-spring-boot-starter:1.7') |
application.properties:
The example application.properties file in spring boot application look like as following
1 2 3 4 5 6 7 |
spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.uri=mongodb://localhost/customer spring.data.mongodb.database=customer spring.data.mongodb.username=root spring.data.mongodb.password=ENC(XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx+hNPrJyQT88= spring.data.mongodb.repositories.enabled=true |
As you can see, spring.data.mongodb.password value is encrypted. But how was this generated?
We can generate it by using jasypt like below (:
1 |
java -cp C:/Users/satish/.gradle/caches/modules-2/files-2.1/org.jasypt/jasypt/1.9.2/91eee489a389faba9fc57bfee75c87c615c19cd7/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="DBOriginalPassword" password=myEncPwd algorithm=PBEWithMD5AndDES |
Usage:
We can use spring.data.mongodb.password property in any spring component as like using any other properties.
Example:
1 2 |
@Value('${spring.data.mongodb.password}') private String mongoPassword |
Running the Application:
when running the application we should provide the value which is used to generate encrypted password i:e, myEncPwd.
1 |
java -jar -Djasypt.encryptor.password=myEncPwd myApp.jar |
Thank You 🙂