From 81c65419688cd1fdbd9eda2c3c725da12d81591d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Thu, 27 Feb 2025 09:06:13 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BB=A3=E7=A0=81=E7=AE=80?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/sensitive/SensitiveType.java | 4 ++++ .../core/tool/sensitive/SensitiveUtil.java | 13 +------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveType.java b/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveType.java index 9f357e0..6c98db8 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveType.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveType.java @@ -17,6 +17,8 @@ package org.springblade.core.tool.sensitive; import lombok.Getter; +import java.util.regex.Pattern; + /** * 脱敏类型枚举. * @@ -36,10 +38,12 @@ public enum SensitiveType { private final String desc; private final String regex; private final String replacement; + private final Pattern pattern; SensitiveType(String desc, String regex, String replacement) { this.desc = desc; this.regex = regex; this.replacement = replacement; + this.pattern = Pattern.compile(regex); } } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveUtil.java index db5d97d..b28e0e3 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/sensitive/SensitiveUtil.java @@ -39,9 +39,6 @@ public class SensitiveUtil { private static final String DEFAULT_REPLACEMENT = "******"; private static final String LINE_SEPARATOR = System.lineSeparator(); - // 预编译的正则表达式 - private static final Map PATTERN_CACHE = new EnumMap<>(SensitiveType.class); - // 预编译的默认配置 private static final SensitiveConfig DEFAULT_CONFIG = SensitiveConfig.builder() .sensitiveTypes(EnumSet.of( @@ -58,13 +55,6 @@ public class SensitiveUtil { .replacement(DEFAULT_REPLACEMENT) .build(); - static { - // 预编译所有内置的正则表达式 - for (SensitiveType type : SensitiveType.values()) { - PATTERN_CACHE.put(type, Pattern.compile(type.getRegex())); - } - } - /** * 使用默认配置处理敏感信息 * @@ -206,8 +196,7 @@ public class SensitiveUtil { private static String processRegexPatterns(String content, Set types) { String result = content; for (SensitiveType type : types) { - Pattern pattern = PATTERN_CACHE.get(type); - result = pattern.matcher(result).replaceAll(type.getReplacement()); + result = type.getPattern().matcher(result).replaceAll(type.getReplacement()); } return result; }