Skip to content

Commit

Permalink
✨ Add ipv4, ipv6 and first mask support
Browse files Browse the repository at this point in the history
  • Loading branch information
lzhpo committed Mar 20, 2024
1 parent c7440dc commit df56c75
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/lzhpo/sensitive/SensitiveConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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;

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/lzhpo/sensitive/util/SensitiveUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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的规则,只显示第一个字符。<br>
* 脱敏前: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);
}
}

0 comments on commit df56c75

Please sign in to comment.