mirror of
https://github.com/chillzhuang/blade-tool
synced 2025-03-13 00:57:24 +08:00
✨ 代码简化
This commit is contained in:
parent
7a160bc8ba
commit
81c6541968
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<SensitiveType, Pattern> 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<SensitiveType> 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user