BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -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;
}
}
@@ -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 {
}
@@ -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;
}
}
@@ -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 {
}
@@ -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");
}
}
@@ -0,0 +1,3 @@
jasypt.encryptor.bean=encryptorBean
encryptedv3.property=ENC(askygdq8PHapYFnlX6WsTwZZOxWInq+i)
@@ -0,0 +1 @@
encrypted.property=ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8)
@@ -0,0 +1 @@
encryptedv2.property=ENC(dQWokHUXXFe+OqXRZYWu22BpXoRZ0Drt)
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -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 CustomJasyptIntegrationTest {
@Autowired
ApplicationContext appCtx;
@Test
public void whenConfiguredExcryptorUsed_ReturnCustomEncryptor() {
Environment environment = appCtx.getBean(Environment.class);
assertEquals("Password@3", environment.getProperty("encryptedv3.property"));
}
}
@@ -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 JasyptSimpleIntegrationTest {
@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());
}
}
@@ -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 JasyptWithStarterIntegrationTest {
@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));
}
}
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.jasypt.Main;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.jasypt.Main;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}