From e440510a23a3012daff1d6b04bb1bbcf493354a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=9F=E6=B0=B8=E8=B6=85?= Date: Fri, 13 Aug 2021 16:59:21 +0800 Subject: [PATCH] update --- 2.x/chapter1-5/pom.xml | 12 ++++++++++ .../chapter15/Chapter15Application.java | 24 ++++++++++++++++--- .../didispace/chapter15/HelloController.java | 14 ----------- .../didispace/chapter15/JasyptExample.java | 18 ++++++++++++++ .../src/main/resources/application.properties | 10 +++++++- .../didispace/chapter15/PropertiesTest.java | 21 ++++++++++++++++ 6 files changed, 81 insertions(+), 18 deletions(-) delete mode 100644 2.x/chapter1-5/src/main/java/com/didispace/chapter15/HelloController.java create mode 100644 2.x/chapter1-5/src/main/java/com/didispace/chapter15/JasyptExample.java create mode 100644 2.x/chapter1-5/src/test/java/com/didispace/chapter15/PropertiesTest.java diff --git a/2.x/chapter1-5/pom.xml b/2.x/chapter1-5/pom.xml index 23e61eaa..35d5fb02 100644 --- a/2.x/chapter1-5/pom.xml +++ b/2.x/chapter1-5/pom.xml @@ -23,6 +23,12 @@ spring-boot-starter-web + + com.github.ulisesbocchio + jasypt-spring-boot-starter + 3.0.3 + + org.projectlombok lombok @@ -41,6 +47,12 @@ org.springframework.boot spring-boot-maven-plugin + + + com.github.ulisesbocchio + jasypt-maven-plugin + 3.0.3 + diff --git a/2.x/chapter1-5/src/main/java/com/didispace/chapter15/Chapter15Application.java b/2.x/chapter1-5/src/main/java/com/didispace/chapter15/Chapter15Application.java index 1547f9b5..df4e07eb 100644 --- a/2.x/chapter1-5/src/main/java/com/didispace/chapter15/Chapter15Application.java +++ b/2.x/chapter1-5/src/main/java/com/didispace/chapter15/Chapter15Application.java @@ -1,13 +1,31 @@ package com.didispace.chapter15; +import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; @SpringBootApplication +@EnableEncryptableProperties public class Chapter15Application { - public static void main(String[] args) { - SpringApplication.run(Chapter15Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Chapter15Application.class, args); + } + + @RestController + static class HelloController { + + @Autowired + private JasyptExample jasyptExample; + + @GetMapping("/hello") + public String hello() { + return "Hello World, " + jasyptExample.getFrom2(); + } + + } } diff --git a/2.x/chapter1-5/src/main/java/com/didispace/chapter15/HelloController.java b/2.x/chapter1-5/src/main/java/com/didispace/chapter15/HelloController.java deleted file mode 100644 index 91236867..00000000 --- a/2.x/chapter1-5/src/main/java/com/didispace/chapter15/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.didispace.chapter15; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class HelloController { - - @RequestMapping("/hello") - public String index() { - return "Hello World"; - } - -} \ No newline at end of file diff --git a/2.x/chapter1-5/src/main/java/com/didispace/chapter15/JasyptExample.java b/2.x/chapter1-5/src/main/java/com/didispace/chapter15/JasyptExample.java new file mode 100644 index 00000000..c87ca75f --- /dev/null +++ b/2.x/chapter1-5/src/main/java/com/didispace/chapter15/JasyptExample.java @@ -0,0 +1,18 @@ +package com.didispace.chapter15; + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Slf4j +@Data +@Component +public class JasyptExample { + + @Value("${com.didispace.from1:}") + private String from1; + @Value("${com.didispace.from2:}") + private String from2; + +} \ No newline at end of file diff --git a/2.x/chapter1-5/src/main/resources/application.properties b/2.x/chapter1-5/src/main/resources/application.properties index 20da7c87..1cbf505e 100644 --- a/2.x/chapter1-5/src/main/resources/application.properties +++ b/2.x/chapter1-5/src/main/resources/application.properties @@ -1,3 +1,11 @@ -com.didispace.from=didi +com.didispace.from1=didi +com.didispace.from2=ENC(1I1oWHryzOJt+Gm81xnGnOUbBUpEBEFYES/NprA/q6ec3jBkU1xlBZWsHCaj71ds) +jasypt.encryptor.password=didispace + +#[INFO] Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator +#[INFO] Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.RandomIvGenerator + +# mvn jasypt:encrypt -Djasypt.encryptor.password=didispace +# mvn jasypt:decrypt -Djasypt.encryptor.password=didispace \ No newline at end of file diff --git a/2.x/chapter1-5/src/test/java/com/didispace/chapter15/PropertiesTest.java b/2.x/chapter1-5/src/test/java/com/didispace/chapter15/PropertiesTest.java new file mode 100644 index 00000000..1185ff4a --- /dev/null +++ b/2.x/chapter1-5/src/test/java/com/didispace/chapter15/PropertiesTest.java @@ -0,0 +1,21 @@ +package com.didispace.chapter15; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@Slf4j +@SpringBootTest +public class PropertiesTest { + + @Autowired + private JasyptExample jasyptExample; + + @Test + public void test() { + log.info("from1 : {}", jasyptExample.getFrom1()); + log.info("from2 : {}", jasyptExample.getFrom2()); + } + +}