Spring Boot and Jasypt Code (#4142)
* Spring Boot and Jasypt Code * Tabs to Spaces in POM * Removed unnecessary files * Formatted Pom
This commit is contained in:
parent
a9f08084ff
commit
1e3b3f27a4
|
@ -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/
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example.jasypt</groupId>
|
||||
<artifactId>JasyptDemo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>JasyptDemo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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,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"));
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue