//1.定义脱敏接口 public interface IReplaceMask { public String maskStr(String str); } import java.util.regex.Matcher; import java.util.regex.Pattern; //2.邮箱脱敏实现脱敏接口 public class EmailMask implements IReplaceMask{ public String maskStr(String email) { Pattern pattern = Pattern.compile("(\\w{3})\\w+([a-zA-Z0-9]{2}@[a-z]+\\.[a-z]+)", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(email); while (matcher.find()) { String group = matcher.group(); String desensitizedEmail = group.replaceAll(".(?=.*[@])", "*"); //System.out.println("原始邮箱地址:" + email); //System.out.println("脱敏后的邮箱地址:" + desensitizedEmail); email=email.replaceAll(group,desensitizedEmail); matcher = pattern.matcher(email); } return email; } } //3.手机号脱敏规则实现脱敏接口 public class PhoneMask implements IReplaceMask{ public String maskStr(String phoneNumber) { String regex = "(\\d{3})\\d{4}(\\d{4})"; return phoneNumber.replaceAll(regex, "$1****$2"); } } import java.util.ArrayList; import java.util.List; //4.脱敏工具类getInstaces包含了手机脱敏和邮箱脱敏,可扩展通过list.add(new 新的脱敏规则类())) public class ReplaceMaskUtils { public static List<IReplaceMask> getInstaces() { List<IReplaceMask> list = new ArrayList<IReplaceMask>(); list.add(new PhoneMask()); list.add(new EmailMask()); return list; } } import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.sun.scenario.effect.impl.prism.PrImage; import java.util.ArrayList; import java.util.List; public class 正则脱敏测试 { public static void main(String[] args) { //1.构造复杂数据结构开始 List<Classes> classesList=new ArrayList<Classes>(); Classes classes = new Classes(); classes.setId("c1"); classes.setName("n1"); List<Student<QianBao>> stus = new ArrayList<Student<QianBao>>(); classes.setStudents(stus); for (int i = 0; i < 3; i++) { Student<QianBao> student = new Student<QianBao>(); student.setStuId("stuId" + i+" 18205943515"); student.setStuName("stuName" + i+"[email protected]"); QianBao q=new QianBao(); q.setqId("stuId"+i+"的钱包ID 18205943512"); q.setQn("stuName"+i+"的钱包Name [email protected]"); student.setData1(q); stus.add(student); } classesList.add(classes); Result<Classes> result=new Result<Classes>(); result.setData(classesList); //1.构造复杂数据结构结束 //2.把复杂数据结构(嵌套泛型) 序列化成字符串 String s = JSON.toJSONString(result); System.out.println(s); //3.对序列化字符串进行脱敏,内部使用链式设计模式,循环替换手机号、邮箱进行脱敏 String string = maskString(s); System.out.println(string); //4.把脱敏后字符串还原成原先的数据结构 Result<Classes> result1 = JSON.parseObject(string, new TypeReference<Result<Classes>>() { }); String s1 = JSON.toJSONString(result1); //5.验证脱敏后的结构对JSON应字符串是否正确 System.out.println("s1==string \t"+s1.equals(string)); } private static String maskString(String str) { List<IReplaceMask> instaces = ReplaceMaskUtils.getInstaces(); for (IReplaceMask instace : instaces) { str = instace.maskStr(str); } return str; } static class Result<T> { List<T> data; public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } } static class Classes { private String id; private String name; private List<Student<QianBao>> students; public List<Student<QianBao>> getStudents() { return students; } public void setStudents(List<Student<QianBao>> students) { this.students = students; } public String getId() { return id; } public String getName() { return name; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } } static class QianBao{ private String qId; private String qn; public String getqId() { return qId; } public void setqId(String qId) { this.qId = qId; } public String getQn() { return qn; } public void setQn(String qn) { this.qn = qn; } } static class Student<T> { private T data1; public T getData1() { return data1; } public void setData1(T data1) { this.data1 = data1; } private String stuId; private String stuName; public String getStuId() { return stuId; } public void setStuId(String stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } } }
//1.定义脱敏接口 public interface IReplaceMask { public String maskStr(String str); } import java.util.regex.Matcher; import java.util.regex.Pattern; //2.邮箱脱敏实现脱敏接口 public class EmailMask implements IReplaceMask{ public String maskStr(String email) { Pattern pattern = Pattern.compile("(\\w{3})\\w+([a-zA-Z0-9]{2}@[a-z]+\\.[a-z]+)", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(email); while (matcher.find()) { String group = matcher.group(); String desensitizedEmail = group.replaceAll(".(?=.*[@])", "*"); //System.out.println("原始邮箱地址:" + email); //System.out.println("脱敏后的邮箱地址:" + desensitizedEmail); email=email.replaceAll(group,desensitizedEmail); matcher = pattern.matcher(email); } return email; } } //3.手机号脱敏规则实现脱敏接口 public class PhoneMask implements IReplaceMask{ public String maskStr(String phoneNumber) { String regex = "(\\d{3})\\d{4}(\\d{4})"; return phoneNumber.replaceAll(regex, "$1****$2"); } } import java.util.ArrayList; import java.util.List; //4.脱敏工具类getInstaces包含了手机脱敏和邮箱脱敏,可扩展通过list.add(new 新的脱敏规则类())) public class ReplaceMaskUtils { public static List<IReplaceMask> getInstaces() { List<IReplaceMask> list = new ArrayList<IReplaceMask>(); list.add(new PhoneMask()); list.add(new EmailMask()); return list; } } import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.sun.scenario.effect.impl.prism.PrImage; import java.util.ArrayList; import java.util.List; public class 正则脱敏测试 { public static void main(String[] args) { //1.构造复杂数据结构开始 List<Classes> classesList=new ArrayList<Classes>(); Classes classes = new Classes(); classes.setId("c1"); classes.setName("n1"); List<Student<QianBao>> stus = new ArrayList<Student<QianBao>>(); classes.setStudents(stus); for (int i = 0; i < 3; i++) { Student<QianBao> student = new Student<QianBao>(); student.setStuId("stuId" + i+" 18205943515"); student.setStuName("stuName" + i+"[email protected]"); QianBao q=new QianBao(); q.setqId("stuId"+i+"的钱包ID 18205943512"); q.setQn("stuName"+i+"的钱包Name [email protected]"); student.setData1(q); stus.add(student); } classesList.add(classes); Result<Classes> result=new Result<Classes>(); result.setData(classesList); //1.构造复杂数据结构结束 //2.把复杂数据结构(嵌套泛型) 序列化成字符串 String s = JSON.toJSONString(result); System.out.println(s); //3.对序列化字符串进行脱敏,内部使用链式设计模式,循环替换手机号、邮箱进行脱敏 String string = maskString(s); System.out.println(string); //4.把脱敏后字符串还原成原先的数据结构 Result<Classes> result1 = JSON.parseObject(string, new TypeReference<Result<Classes>>() { }); String s1 = JSON.toJSONString(result1); //5.验证脱敏后的结构对JSON应字符串是否正确 System.out.println("s1==string \t"+s1.equals(string)); } private static String maskString(String str) { List<IReplaceMask> instaces = ReplaceMaskUtils.getInstaces(); for (IReplaceMask instace : instaces) { str = instace.maskStr(str); } return str; } static class Result<T> { List<T> data; public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } } static class Classes { private String id; private String name; private List<Student<QianBao>> students; public List<Student<QianBao>> getStudents() { return students; } public void setStudents(List<Student<QianBao>> students) { this.students = students; } public String getId() { return id; } public String getName() { return name; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } } static class QianBao{ private String qId; private String qn; public String getqId() { return qId; } public void setqId(String qId) { this.qId = qId; } public String getQn() { return qn; } public void setQn(String qn) { this.qn = qn; } } static class Student<T> { private T data1; public T getData1() { return data1; } public void setData1(T data1) { this.data1 = data1; } private String stuId; private String stuName; public String getStuId() { return stuId; } public void setStuId(String stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } } } 标签:return,String,List,new,链式,java,public,脱敏 From: https://www.cnblogs.com/kexb/p/17968640