java 日志脱敏处理
package com.itheima.utils; import org.apache.commons.lang3.StringUtils; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AnonymityTools { // 邮箱正则 private static Pattern emailPattern = Pattern.compile("(\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b)"); // 手机正则 private static Pattern phonePattern = Pattern.compile("(\\b1[0-9]\\d{9}\\b)"); public static String anonymize(Matcher matcher, Function<String, String> anonymizeFunction) { // 匹配正则 StringBuffer result = new StringBuffer(); while (matcher.find()) { int start = matcher.start(); String hitResult = matcher.group(1); matcher.appendReplacement(result, anonymizeFunction.apply(hitResult)); } matcher.appendTail(result); return result.toString(); } public static String anonymizeEmail(String content) { if (StringUtils.isEmpty(content)) { return content; } return anonymize(emailPattern.matcher(content), new Function<String, String>() { @Override public String apply(String hitResult) { // todo email匿名化规则 int atIndex = hitResult.indexOf('@'); String username = hitResult.substring(0, atIndex); String domain = hitResult.substring(atIndex); return username.substring(0, 3) + "*****" + domain; } }); } public static String anonymizePhone(String content) { if (StringUtils.isEmpty(content)) { return content; } // todo 手机号匿名化规则 return anonymize(phonePattern.matcher(content), hitResult -> hitResult.substring(0, 3) + "****" + hitResult.substring(7)); } public static void main(String[] args) { String content = "longlinji@huawei.com,longlinji@huawei.com,longlinji@huawei.com,This is 15361826615 test email: test@example.com and phone: 13812345678,12345678911-12345678911*12345678911+12345678911"; String anonymizeEmail = anonymizeEmail(content); String anonymizePhone = anonymizePhone(anonymizeEmail); System.out.println(anonymizePhone); } }
测试用例: 1. 输入空字符串 2. 输入不含邮箱和手机号的字符串 3. 输入仅含邮箱的字符串,包括多个邮箱,邮箱在字符串开始、中间、结尾,邮箱前后有其他字符等情况 4. 输入仅含手机号的字符串,包括多个手机号,手机号在字符串开始、中间、结尾,手机号前后有其他字符等情况 5. 输入既含手机号又含邮箱的字符串,包括多个手机号和邮箱,电话和邮箱在字符串开始、中间、结尾,前后有其他字符等情况 6. 输入邮箱和手机号混合,并且邮箱和手机号重叠在一起的情况 7. 输入邮箱或手机号含有特殊字符(如空格、货币符号等)的情况
单元测试: @Test public void testAnonymizeEmail() { String content = "longlinji@huawei.com,longlinji@huawei.com,longlinji@huawei.com,This is a test email: test@example.com and phone: 13812345678,12345678911-12345678911*12345678911 12345678911"; String expected = "longl*****i@huawei.com,longl*****i@huawei.com,longl*****i@huawei.com,This is a test email: t*****@example.com and phone: 13812345678,12345678911-12345678911*12345678911 12345678911"; String actual = AnonymityTools.anonymizeEmail(content); assertEquals(expected, actual); } @Test public void testAnonymizePhone() { String content = "longlinji@huawei.com,longlinji@huawei.com,longlinji@huawei.com,This is a test email: test@example.com and phone: 13812345678,12345678911-12345678911*12345678911 12345678911"; String expected = "longlinji@huawei.com,longlinji@huawei.com,longlinji@huawei.com,This is a test email: test@example.com and phone: 138****5678,123****8911-123****8911*123****8911 123****8911"; String actual = AnonymityTools.anonymizePhone(content); assertEquals(expected, actual); }
package com.itheima; import com.itheima.utils.AnonymityTools; import org.junit.Assert; import org.junit.Test; public class AnonymityToolsTest { @Test public void testAnonymizeEmail() { String expected = "123*****@test.com"; String actual = AnonymityTools.anonymizeEmail("1234567890@test.com"); Assert.assertEquals(expected, actual); } @Test public void testAnonymizePhone() { String expected = "138****1234"; String actual = AnonymityTools.anonymizePhone("13812341234"); Assert.assertEquals(expected, actual); } }