user1340582 user1340582 - 3 years ago 221
Java Question

Spring Boot how to hide passwords in properties file

Spring Boot uses the properties file, and at least by default, the passwords are in plain text. Is it possible to somehow hide/decrypt these?

Answer Source

You can use Jasypt to encrypt properties, so you could have your property like this:


Jasypt allows you to encrypt your properties using different algorithms, once you get the encrypted property you put inside the ENC(...). For instance, you can encrypt this way through Jasypt using the terminal:

encrypted-pwd$ java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="contactspassword" password=supersecretz algorithm=PBEWithMD5AndDES


Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 24.45-b08


algorithm: PBEWithMD5AndDES
input: contactspassword
password: supersecretz



To easily configure it with Spring Boot you can use its starter:


Keep in mind, that you will need to start your application using the same password you used to encrypt the properties. So, you can start your app this way:

mvn -Djasypt.encryptor.password=supersecretz spring-boot:run

You can check below link for more details:

To use your encrypted properties in your app just use it as usual, use either method you like:

Using @Value annotation

private String password;

Or using Environment

private Environment environment;

public void doSomething(Environment env) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download