From becbb3401107f332bb0f91429521d2b25088db8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Mon, 10 Mar 2025 12:46:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(encrypt):=20=E5=B0=86=20`KEY=5FEXTRACT?= =?UTF-8?q?ORS`=20=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=8E=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=9D=97=E6=94=B9=E4=B8=BA=20`Map.of`=20=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此次修改将 `KEY_EXTRACTORS` 的初始化方式从静态块改为 `Map.of`,简化了代码结构并提高了可读性。 --- .../api/encrypt/util/ApiCryptoUtil.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java index a0883ee7b..59ecf3750 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/util/ApiCryptoUtil.java @@ -23,6 +23,7 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.Map; import java.util.function.Function; @@ -38,15 +39,16 @@ import java.util.function.Function; @UtilityClass public class ApiCryptoUtil { - /** - * 密钥提取器 - */ - public static final Map> KEY_EXTRACTORS = Map.of( - EncryptType.AES, ApiEncryptProperties::getAesKey, - EncryptType.RSA, ApiEncryptProperties::getRsaPrivateKey, - EncryptType.DES, ApiEncryptProperties::getDesKey, - EncryptType.SM4, ApiEncryptProperties::getSm4Key - ); + public static final Map> KEY_EXTRACTORS; + + static { + Map> map = new HashMap<>(); + map.put(EncryptType.AES, ApiEncryptProperties::getAesKey); + map.put(EncryptType.RSA, ApiEncryptProperties::getRsaPrivateKey); + map.put(EncryptType.DES, ApiEncryptProperties::getDesKey); + map.put(EncryptType.SM4, ApiEncryptProperties::getSm4Key); + KEY_EXTRACTORS = map; + } /**