Details how to integrate jasypt (http://www.jasypt.org/) into a Spring Boot project to provide encryption for externalized properties.
Step-by-step guide for integrating Jasypt into a Spring Boot Project
- An example project can be found at GitHub
- Include the dependency (check for an updated version):
compile group: ‘com.github.ulisesbocchio’, name: ‘jasypt-spring-boot-starter’, version: ‘1.16’
- The @SpringBootApplication annotation will enable encryptable properties across the entire Spring Environment (This means any system property, environment property, command line argument, application.properties, yaml properties, and any other custom property sources can contain encrypted properties)
- Now you just need to include the following in your internal properties file (the one that will be compiled and not easily accessible):
** These values will be used to decrypt the encrypted property during runtime, these values are also used initially for encrypting the password using the shell script shown in the next step-by-step below.
In yaml format:
jasypt: encryptor: algorithm: PBEWithMD5AndDES password: secretkey
- Now encrypt your password using the steps below and include the encrypted password in an external file structure like the one shown below:
Step-by-step guide for encrypting a password
- Download jasypt-shell-scripts.zip locally to your computer
- Open terminal and go to the following location and run the command shown below
**PARAMETERS: (input: The password you want to encrypt, password: The key to encrypt/decrypt the password, algorithm: the algorithm used to encrypt)
- Copy the encrypted password shown below OUTPUT and copy it to the externalized properties file using the following format:
- Restart the Spring Boot application and confirm everything is running correctly.
Using Docker to isolate and protect the application
- Create a dockerfile like the following:
FROM openjdk:8-jdk-alpine VOLUME /tmp ADD jasypt-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
– The ADD command copies files from your local machine to the docker image
– The config/application.properties is where you can place your encrypted password
– To reduce Tomcat startup time we added a system property pointing to “/dev/urandom” as a source of entropy.
- Now you can run the following command to read in the config/application.properties file that contains the encrypted password:
docker run --rm -p 8080:8080 -v /LOCALHOST/PATH/TO/config/application.properties:/config/application.properties -t jasypt-kotlin-test