diff --git a/spring-boot-jasypt/.gitignore b/spring-boot-jasypt/.gitignore new file mode 100644 index 0000000000..82eca336e3 --- /dev/null +++ b/spring-boot-jasypt/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-jasypt/pom.xml b/spring-boot-jasypt/pom.xml new file mode 100644 index 0000000000..e094f0b061 --- /dev/null +++ b/spring-boot-jasypt/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.example.jasypt + JasyptDemo + 0.0.1-SNAPSHOT + jar + + JasyptDemo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + 2.0.0 + + + + com.github.ulisesbocchio + jasypt-spring-boot + 2.0.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/Main.java b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/Main.java new file mode 100644 index 0000000000..0cdb5d4f1b --- /dev/null +++ b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/Main.java @@ -0,0 +1,35 @@ +package com.baeldung.jasypt; + +import org.jasypt.encryption.StringEncryptor; +import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; +import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; + +@SpringBootApplication +@ComponentScan(basePackages = { "com.baeldung.jasypt" }) +@EnableEncryptableProperties +public class Main { + public static void main(String[] args) { + new SpringApplicationBuilder().sources(Main.class).run(args); + } + + @Bean(name = "encryptorBean") + public StringEncryptor stringEncryptor() { + PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); + SimpleStringPBEConfig config = new SimpleStringPBEConfig(); + config.setPassword("password"); + config.setAlgorithm("PBEWithMD5AndDES"); + config.setKeyObtentionIterations("1000"); + config.setPoolSize("1"); + config.setProviderName("SunJCE"); + config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); + config.setStringOutputType("base64"); + encryptor.setConfig(config); + return encryptor; + } +} diff --git a/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/AppConfigForJasyptSimple.java b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/AppConfigForJasyptSimple.java new file mode 100644 index 0000000000..41681cb8ca --- /dev/null +++ b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/AppConfigForJasyptSimple.java @@ -0,0 +1,10 @@ +package com.baeldung.jasypt.simple; + +import org.springframework.context.annotation.Configuration; + +import com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySource; + +@Configuration +@EncryptablePropertySource(value = "encryptedv2.properties") +public class AppConfigForJasyptSimple { +} diff --git a/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/PropertyServiceForJasyptSimple.java b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/PropertyServiceForJasyptSimple.java new file mode 100644 index 0000000000..def4808b8e --- /dev/null +++ b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/simple/PropertyServiceForJasyptSimple.java @@ -0,0 +1,15 @@ +package com.baeldung.jasypt.simple; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class PropertyServiceForJasyptSimple { + + @Value("${encryptedv2.property}") + private String property; + + public String getProperty() { + return property; + } +} diff --git a/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/AppConfigForJasyptStarter.java b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/AppConfigForJasyptStarter.java new file mode 100644 index 0000000000..4db90ed5dc --- /dev/null +++ b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/AppConfigForJasyptStarter.java @@ -0,0 +1,9 @@ +package com.baeldung.jasypt.starter; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@PropertySource(value = "encrypted.properties") +public class AppConfigForJasyptStarter { +} diff --git a/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/PropertyServiceForJasyptStarter.java b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/PropertyServiceForJasyptStarter.java new file mode 100644 index 0000000000..725720ebff --- /dev/null +++ b/spring-boot-jasypt/src/main/java/com/baeldung/jasypt/starter/PropertyServiceForJasyptStarter.java @@ -0,0 +1,20 @@ +package com.baeldung.jasypt.starter; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Service; + +@Service +public class PropertyServiceForJasyptStarter { + + @Value("${encrypted.property}") + private String property; + + public String getProperty() { + return property; + } + + public String getPasswordUsingEnvironment(Environment environment) { + return environment.getProperty("encrypted.property"); + } +} diff --git a/spring-boot-jasypt/src/main/resources/application.properties b/spring-boot-jasypt/src/main/resources/application.properties new file mode 100644 index 0000000000..ea61c4d846 --- /dev/null +++ b/spring-boot-jasypt/src/main/resources/application.properties @@ -0,0 +1,3 @@ +jasypt.encryptor.bean=encryptorBean +encryptedv3.property=ENC(askygdq8PHapYFnlX6WsTwZZOxWInq+i) + diff --git a/spring-boot-jasypt/src/main/resources/encrypted.properties b/spring-boot-jasypt/src/main/resources/encrypted.properties new file mode 100644 index 0000000000..0188fc392c --- /dev/null +++ b/spring-boot-jasypt/src/main/resources/encrypted.properties @@ -0,0 +1 @@ +encrypted.property=ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8) \ No newline at end of file diff --git a/spring-boot-jasypt/src/main/resources/encryptedv2.properties b/spring-boot-jasypt/src/main/resources/encryptedv2.properties new file mode 100644 index 0000000000..6785149718 --- /dev/null +++ b/spring-boot-jasypt/src/main/resources/encryptedv2.properties @@ -0,0 +1 @@ +encryptedv2.property=ENC(dQWokHUXXFe+OqXRZYWu22BpXoRZ0Drt) diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java new file mode 100644 index 0000000000..58c2dc7bb2 --- /dev/null +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java @@ -0,0 +1,27 @@ +package com.baeldung.jasypt; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.jasypt.Main; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {Main.class}) +public class CustomJasyptTest { + + @Autowired + ApplicationContext appCtx; + + @Test + public void whenConfiguredExcryptorUsed_ReturnCustomEncryptor() { + Environment environment = appCtx.getBean(Environment.class); + assertEquals("Password@3", environment.getProperty("encryptedv3.property")); + } +} diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java new file mode 100644 index 0000000000..f9b66b5044 --- /dev/null +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java @@ -0,0 +1,28 @@ +package com.baeldung.jasypt; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.jasypt.simple.PropertyServiceForJasyptSimple; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class JasyptSimpleTest { + + @Autowired + ApplicationContext appCtx; + + @Test + public void whenDecryptedPasswordNeeded_GetFromService() { + System.setProperty("jasypt.encryptor.password", "password"); + PropertyServiceForJasyptSimple service = appCtx.getBean(PropertyServiceForJasyptSimple.class); + assertEquals("Password@2", service.getProperty()); + } + +} diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java new file mode 100644 index 0000000000..d246c21036 --- /dev/null +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java @@ -0,0 +1,30 @@ +package com.baeldung.jasypt; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.jasypt.starter.PropertyServiceForJasyptStarter; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class JasyptWithStarterTest { + + @Autowired + ApplicationContext appCtx; + + @Test + public void whenDecryptedPasswordNeeded_GetFromService() { + System.setProperty("jasypt.encryptor.password", "password"); + PropertyServiceForJasyptStarter service = appCtx.getBean(PropertyServiceForJasyptStarter.class); + assertEquals("Password@1", service.getProperty()); + Environment environment = appCtx.getBean(Environment.class); + assertEquals("Password@1", service.getPasswordUsingEnvironment(environment)); + } +}