1、字符脱敏工具类
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class DataDesensitizationUtils {
public static final String STAR_1 = "*";
public static final String STAR_2 = "**";
public static final String STAR_3 = "***";
public static final String STAR_4 = "****";
public static String mobileDesensitize(String mobile) {
if (StringUtils.isBlank(mobile)) {
return "";
}
String right = StringUtils.right(mobile, 4);
String leftPad = StringUtils.leftPad(right, StringUtils.length(mobile), STAR_1);
String removeStart = StringUtils.removeStart(leftPad, STAR_3);
String left = StringUtils.left(mobile, 3);
return left.concat(removeStart);
}
public static String nameDesensitize(String name) {
if (StringUtils.isBlank(name)) {
return "";
}
int length = 3;
if (name.length() <= length) {
int len = name.length() - 1;
String right = StringUtils.right(name, len);
return STAR_1.concat(right);
}
int len = name.length() - 2;
String right = StringUtils.right(name, len);
return STAR_2.concat(right);
}
public static String detailAddressDesensitize(String detailAddress) {
int length = 10;
String desensitizationText = STAR_4;
if (StringUtils.isBlank(detailAddress) || detailAddress.length() <= length) {
return desensitizationText;
} else {
StringBuilder address = new StringBuilder(detailAddress);
int start = address.length() - 10;
int end = address.length();
return address.replace(start, end, desensitizationText).toString();
}
}
public static String contentDesensitize(int start, int end, String content, int maxStarSize) {
if (StringUtils.isBlank(content)) {
return "";
}
start = Math.max(0, start);
end = Math.min(content.length(), end);
if (end < start) {
return content;
}
int min = Math.min(end - start, maxStarSize);
int count = Math.max(0, min);
String repeat = STAR_1.repeat(count);
return new StringBuilder(content)
.replace(start, end, repeat)
.toString();
}
public static String contentDesensitize(int start, int end, String content) {
return contentDesensitize(start, end, content, end - start);
}
}
2、测试工具类
@Slf4j
@ExtendWith(MockitoExtension.class)
public class DataDesensitizationUtilsTest {
@Test
public void testMobileDesensitize() {
String mobile = "13011112222";
String result = DataDesensitizationUtils.mobileDesensitize(mobile);
log.info("手机号脱敏结果:{}", result);
}
@Test
public void testNameDesensitize() {
String name = "诸葛孔明";
String result = DataDesensitizationUtils.nameDesensitize(name);
log.info("姓名脱敏结果:{}", result);
}
@Test
public void testAddressDesensitize() {
String address = "北京首钢国际会展中心9号馆、10号馆";
String result = DataDesensitizationUtils.detailAddressDesensitize(address);
log.info("地址脱敏结果:{}", result);
}
@Test
public void testStringUtils() {
String param = "北京首钢国际会展中心9号馆、10号馆";
String result1 = StringUtils.left(param, 3);
log.info("处理结果1:{}", result1);
String result2 = StringUtils.right(param, 4);
log.info("处理结果2:{}", result2);
String result3 = StringUtils.leftPad(result2, param.length(), "*");
log.info("处理结果3:{}", result3);
}
@Test
public void testContentDesensitize() {
String param = "诸葛孔明";
String result1 = DataDesensitizationUtils.contentDesensitize(1, 7, param);
log.info("处理结果1:{}", result1);
String result2 = DataDesensitizationUtils.contentDesensitize(1, param.length() - 7, param, 6);
log.info("处理结果2:{}", result2);
String result3 = DataDesensitizationUtils.contentDesensitize(0, param.length() + 7, param, 6);
log.info("处理结果3:{}", result3);
}
}
3、测试结果:
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testNameDesensitize():30 - 姓名脱敏结果:**孔明
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testMobileDesensitize():23 - 手机号脱敏结果:130****2222
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testAddressDesensitize():37 - 地址脱敏结果:北京首钢国际会展****
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testStringUtils():44 - 处理结果1:北京首
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testStringUtils():47 - 处理结果2:10号馆
2024-10-31 09:36:11 [INFO ] [main] - [] - com.summer.toolkit.service.DataDesensitizationUtilsTest.testStringUtils():50 - 处理结果3:**************10号馆