在枚举中配置要脱敏的类型
正数或者倒数
从第n位 到 n+m位
package com.base.infrastructure.common.enums;
/**
* The enum Desensitize type.
* 例如 asc=true,start=2,end=4 ,那么就是从正数第2位 至 正数第4位脱敏 1***5678
* 例如 asc=false,start=2,end=4 ,那么就是从倒数第2位 至 倒数第4位脱敏 1234***8
*/
public enum DesensitizeType {
TEST1(false, 1, 4),
TEST2(true, 5, 7),
TEST3(false,4,5);
private Boolean asc;//true 正数 ,false 倒数
private int start;//开始脱敏的位
private int end;//结束脱敏的位
DesensitizeType(boolean asc, int start, int end) {
this.asc = asc;
this.start = start;
this.end = end;
}
public Boolean getASC() {
return this.asc;
}
public int getStart() {
return this.start;
}
public int getEnd() {
return this.end;
}
}
二、脱敏工具类
对String类型的数据进行脱敏
package com.base.infrastructure.util;
import com.base.infrastructure.common.enums.DesensitizeType;
/**
* The type Desensitize util.
* 脱敏工具
*/
public class DesensitizeUtil {
public static String desensitize(String value, DesensitizeType desensitizeType) {
try {
Boolean asc = desensitizeType.getASC();
int start = desensitizeType.getStart();
int end = Math.min(desensitizeType.getEnd(), value.length());
String s = "";
for (int i = 0; i <= end - start; i++) {
s += "*";
}
if (asc) {
return value.substring(0, start - 1) + s + value.substring(end);
} else {
return value.substring(0, value.length() - end) + s + value.substring(value.length() - start + 1);
}
} catch (StringIndexOutOfBoundsException e) {
return value;
}
}
public static void main(String[] args) {
String desensitize = DesensitizeUtil.desensitize("1234567", DesensitizeType.TEST1);
System.out.println(desensitize);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容