diff --git a/src/main/java/com/lzhpo/sensitive/SensitiveConstants.java b/src/main/java/com/lzhpo/sensitive/SensitiveConstants.java index 210d14a..eba64d4 100644 --- a/src/main/java/com/lzhpo/sensitive/SensitiveConstants.java +++ b/src/main/java/com/lzhpo/sensitive/SensitiveConstants.java @@ -27,6 +27,12 @@ public final class SensitiveConstants { /** 默认脱敏替换符 */ public static final char REPLACER = '*'; + /** IPV4分隔符 */ + public static final char SEPARATOR_IPV4 = '.'; + + /** IPV6分隔符 */ + public static final char SEPARATOR_IPV6 = ':'; + /** 不处理脱敏保留的标志 */ public static final int NOP_KEEP = -1; diff --git a/src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java b/src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java index 9dd457f..0ea97b2 100644 --- a/src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java +++ b/src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java @@ -106,6 +106,46 @@ public String apply(SensitiveWrapper wrapper) { } }, + /** IPV4 */ + IPV4() { + @Override + public String apply(SensitiveWrapper wrapper) { + return SensitiveUtils.ipv4(wrapper.getFieldValue(), wrapper.getReplacer()); + } + }, + + /** IPV6 */ + IPV6() { + @Override + public String apply(SensitiveWrapper wrapper) { + return SensitiveUtils.ipv6(wrapper.getFieldValue(), wrapper.getReplacer()); + } + }, + + /** Only show the first one */ + FIRST_MASK() { + @Override + public String apply(SensitiveWrapper wrapper) { + return SensitiveUtils.firstMask(wrapper.getFieldValue(), wrapper.getReplacer()); + } + }, + + /** Clear to null */ + CLEAR_TO_NULL() { + @Override + public String apply(SensitiveWrapper wrapper) { + return null; + } + }, + + /** Clear to empty */ + CLEAR_TO_EMPTY() { + @Override + public String apply(SensitiveWrapper wrapper) { + return CharSequenceUtil.EMPTY; + } + }, + /** Customize sensitive keep length */ CUSTOMIZE_FILTER_WORDS() { @Override diff --git a/src/main/java/com/lzhpo/sensitive/util/SensitiveUtils.java b/src/main/java/com/lzhpo/sensitive/util/SensitiveUtils.java index 61e56d3..bb1776a 100644 --- a/src/main/java/com/lzhpo/sensitive/util/SensitiveUtils.java +++ b/src/main/java/com/lzhpo/sensitive/util/SensitiveUtils.java @@ -277,4 +277,48 @@ public static String bankCard(String bankCardNo, char replacer) { buf.append(CharPool.SPACE).append(trimBankCardNo, length - 4, length); return buf.toString(); } + + /** + * IPv4脱敏,如:脱敏前:192.0.2.1;脱敏后:192.*.*.*。 + * + * @param ipv4 IPv4地址 + * @return 脱敏后的地址 + */ + public static String ipv4(String ipv4, char replacer) { + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < 3; i++) { + builder.append(SensitiveConstants.SEPARATOR_IPV4); + builder.append(replacer); + } + return CharSequenceUtil.subBefore(ipv4, SensitiveConstants.SEPARATOR_IPV4, false) + builder; + } + + /** + * IPv4脱敏,如:脱敏前:2001:0db8:86a3:08d3:1319:8a2e:0370:7344;脱敏后:2001:*:*:*:*:*:*:* + * + * @param ipv6 IPv4地址 + * @return 脱敏后的地址 + */ + public static String ipv6(String ipv6, char replacer) { + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < 7; i++) { + builder.append(SensitiveConstants.SEPARATOR_IPV6); + builder.append(replacer); + } + return CharSequenceUtil.subBefore(ipv6, SensitiveConstants.SEPARATOR_IPV6, false) + builder; + } + + /** + * 定义了一个first_mask的规则,只显示第一个字符。
+ * 脱敏前:123456789;脱敏后:1********。 + * + * @param str 字符串 + * @return 脱敏后的字符串 + */ + public static String firstMask(String str, char replacer) { + if (CharSequenceUtil.isBlank(str)) { + return CharSequenceUtil.EMPTY; + } + return CharSequenceUtil.replace(str, 1, str.length(), replacer); + } }