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
This commit is contained in:
Zihlu Wang
2023-09-12 08:35:35 +08:00
parent f28de398fe
commit c9735c5290
@@ -45,18 +45,22 @@ import java.util.Optional;
* app.example-properties=Sample Value * app.example-properties=Sample Value
* *
* # encrypted with key 3856faef0d2d4f33 * # encrypted with key 3856faef0d2d4f33
* app.example-properties=pe:t4YBfv8M9ZmTzWgTi2gJqg== * app.example-properties=pg:t4YBfv8M9ZmTzWgTi2gJqg==
* </pre> * </pre>
* Then, add the command line arguments like {@code --pe.key=3856faef0d2d4f33}. * Then, add the command line arguments like {@code --pe.key=3856faef0d2d4f33}.
* <p> * <p>
* This class is extracted from <a href="https://baomidou.com/pages/e0a5ce/" * This class is extracted from <a href="https://baomidou.com/pages/e0a5ce/"
* >MyBatis-Plus</a>. * >MyBatis-Plus</a>.
* <p>
* The prefix to specify the encrypted value is {@code pg}.
* *
* @author hubin@baomidou * @author hubin@baomidou
* @see org.springframework.boot.env.EnvironmentPostProcessor * @see org.springframework.boot.env.EnvironmentPostProcessor
*/ */
public class PropertyGuard implements EnvironmentPostProcessor { public class PropertyGuard implements EnvironmentPostProcessor {
private final String PREFIX = "pg";
/** /**
* Process the encryption environment variables. * Process the encryption environment variables.
* *
@@ -69,7 +73,7 @@ public class PropertyGuard implements EnvironmentPostProcessor {
var encryptionKey = ""; var encryptionKey = "";
for (var ps : environment.getPropertySources()) { for (var ps : environment.getPropertySources()) {
if (ps instanceof SimpleCommandLinePropertySource source) { if (ps instanceof SimpleCommandLinePropertySource source) {
encryptionKey = source.getProperty("pe.key"); encryptionKey = source.getProperty("%s.key".formatted(PREFIX));
break; break;
} }
} }
@@ -80,7 +84,7 @@ public class PropertyGuard implements EnvironmentPostProcessor {
if (propertySources instanceof OriginTrackedMapPropertySource source) { if (propertySources instanceof OriginTrackedMapPropertySource source) {
for (var name : source.getPropertyNames()) { for (var name : source.getPropertyNames()) {
if (source.getProperty(name) instanceof String str) { 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)); map.put(name, AesUtil.decrypt(str.substring(3), encryptionKey));
} }
} }