From c9735c52901d4765289698b95b0f8e423168c3e0 Mon Sep 17 00:00:00 2001 From: Zihlu Wang Date: Tue, 12 Sep 2023 08:35:35 +0800 Subject: [PATCH] refactor(property-guard): Changed encryption mark from "pe:" to "pg:" BREAKING CHANGE: Changed the encryption mark from "pe:" to "pg:", this version is not compatable with v1.1.0-alpha --- .../propertyguard/autoconfiguration/PropertyGuard.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/property-guard-spring-boot-starter/src/main/java/cn/org/codecrafters/propertyguard/autoconfiguration/PropertyGuard.java b/property-guard-spring-boot-starter/src/main/java/cn/org/codecrafters/propertyguard/autoconfiguration/PropertyGuard.java index e02b6b1..a5059d6 100644 --- a/property-guard-spring-boot-starter/src/main/java/cn/org/codecrafters/propertyguard/autoconfiguration/PropertyGuard.java +++ b/property-guard-spring-boot-starter/src/main/java/cn/org/codecrafters/propertyguard/autoconfiguration/PropertyGuard.java @@ -45,18 +45,22 @@ import java.util.Optional; * app.example-properties=Sample Value * * # encrypted with key 3856faef0d2d4f33 - * app.example-properties=pe:t4YBfv8M9ZmTzWgTi2gJqg== + * app.example-properties=pg:t4YBfv8M9ZmTzWgTi2gJqg== * * Then, add the command line arguments like {@code --pe.key=3856faef0d2d4f33}. *

* This class is extracted from MyBatis-Plus. + *

+ * The prefix to specify the encrypted value is {@code pg}. * * @author hubin@baomidou * @see org.springframework.boot.env.EnvironmentPostProcessor */ public class PropertyGuard implements EnvironmentPostProcessor { + private final String PREFIX = "pg"; + /** * Process the encryption environment variables. * @@ -69,7 +73,7 @@ public class PropertyGuard implements EnvironmentPostProcessor { var encryptionKey = ""; for (var ps : environment.getPropertySources()) { if (ps instanceof SimpleCommandLinePropertySource source) { - encryptionKey = source.getProperty("pe.key"); + encryptionKey = source.getProperty("%s.key".formatted(PREFIX)); break; } } @@ -80,7 +84,7 @@ public class PropertyGuard implements EnvironmentPostProcessor { if (propertySources instanceof OriginTrackedMapPropertySource source) { for (var name : source.getPropertyNames()) { if (source.getProperty(name) instanceof String str) { - if (str.startsWith("pe:")) { + if (str.startsWith("%s:".formatted(PREFIX))) { map.put(name, AesUtil.decrypt(str.substring(3), encryptionKey)); } }