java 对人名和电话 脱敏-replaceAll

学习了《正则匹配人名》和《正则匹配电话号码》,如果要一起进行脱敏处理,改怎么做? 脱敏的,考虑配置规则,进行匹配的方式进行处理:

脱敏规则:

DesensitizationRules

java 复制代码
@Data
public class DesensitizationRules {
    /**
     *  匹配的字段名称
     */
    public String fieldName;

    /**
     *  是否开启匹配 true 是 false 否
     */
    public Boolean enabled;

    /**
     *  匹配的表达式
     */
    public String regularExpr;

    /**
     *  替换的表达式
     */
    public String replaceExpr;
}

数据模拟:

java 复制代码
    public static List<DesensitizationRules> mockConfig(){
        String data = "[\n" +
                "\t{\n" +
                "\t\t\"fieldName\": \"staffName\",\n" +
                "\t\t\"enabled\": true,\n" +
                "\t\t\"regularExpr\": \"(\\\\S)([a-zA-Z0-9_\\\\s\\\\u4e00-\\\\u9fa5]+)\",\n" +
                "\t\t\"replaceExpr\": \"$1**\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"fieldName\": \"mobile\",\n" +
                "\t\t\"enabled\": true,\n" +
                "\t\t\"regularExpr\": \"(\\\\d{3})\\\\d{4}(\\\\d{4})\",\n" +
                "\t\t\"replaceExpr\": \"$1****$2\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"fieldName\": \"tel\",\n" +
                "\t\t\"enabled\": false,\n" +
                "\t\t\"regularExpr\": \"(\\\\d{3})\\\\d{4}(\\\\d{4})\",\n" +
                "\t\t\"replaceExpr\": \"$1****$2\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"fieldName\": \"email\",\n" +
                "\t\t\"enabled\": true,\n" +
                "\t\t\"regularExpr\": \"(\\\\w?)(\\\\w+)(\\\\w)(@\\\\w+\\\\.[a-z]+(\\\\.[a-z]+)?)\",\n" +
                "\t\t\"replaceExpr\": \"$1****$3$4\"\n" +
                "\t}\n" +
                "]";
        DesensitizationRules single;
        List<DesensitizationRules> rules = new ArrayList<>();
        for(Object map: JSONArray.parseArray(data)){
            single= JSON.parseObject(JSONObject.toJSONString(map),DesensitizationRules.class);
            rules.add(single);
        }
        return rules;
    }

测试数据:

FlowStaff

java 复制代码
@Data
public class FlowStaff {
    private String staffName;
    private String phs;
    private String email;
    private String mobile;
    private String tel;
    private String orgId;
    private String orgName;
    private String staffId;
    private String staffaName;
    private String regionName;
    private String regionId;
}

模拟数据:

java 复制代码
     public static List<FlowStaff> mockData(){
        String receiveStr = "[\n" +
                "\t{\n" +
                "\t\t\"staffName\": \"张三飞\",\n" +
                "\t\t\"phs\": null,\n" +
                "\t\t\"email\": \"13456789@qq.com\",\n" +
                "\t\t\"mobile\": \"18920220711\",\n" +
                "\t\t\"tel\": \"18920220711,18955462232\",\n" +
                "\t\t\"orgName\": \"组织机构/公司领导部门\",\n" +
                "\t\t\"staffId\": \"1\",\n" +
                "\t\t\"staffaName\": null,\n" +
                "\t\t\"regionName\": \"福建\",\n" +
                "\t\t\"orgId\": 59\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"staffName\": \"Luck Kevin\",\n" +
                "\t\t\"phs\": null,\n" +
                "\t\t\"email\": \"12345678@qq.com\",\n" +
                "\t\t\"mobile\": \"18920228733\",\n" +
                "\t\t\"tel\": \"18920228733,18955462252\",\n" +
                "\t\t\"orgName\": \"组织机构/公司领导部门\",\n" +
                "\t\t\"staffId\": \"2\",\n" +
                "\t\t\"staffaName\": null,\n" +
                "\t\t\"regionName\": \"福建\",\n" +
                "\t\t\"orgId\": 59\n" +
                "\t}\n" +
                "]";
        System.out.println(receiveStr);
         FlowStaff single;
        List<FlowStaff> flowStaffs = new ArrayList<>();
        for(Object map: JSONArray.parseArray(receiveStr)){
            single= JSON.parseObject(JSONObject.toJSONString(map),FlowStaff.class);
            flowStaffs.add(single);
        }
        return flowStaffs;
    }

处理:

根据规则,先获取配置中enabled 为true,然后对数据进行依次替换脱敏:

java 复制代码
     public static void main(String args[]) {
        List<DesensitizationRules> rules = mockConfig();
        List<FlowStaff> receivers = mockData();
        // 过滤需要脱敏的enable = true的配置
        List<DesensitizationRules> filterRules = ListUtils.emptyIfNull(rules).stream().filter(DesensitizationRules::getEnabled).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(filterRules)){
            System.out.println("not have the config rule!");
            return;
        }
        String receiversStr = JSONObject.toJSONString(receivers);
        for (DesensitizationRules value : filterRules) {
            receiversStr = replaceCommon(receiversStr,  value.getFieldName(), value.getRegularExpr(),  value.getReplaceExpr());
        }
        System.out.println("============ mask ============ ");
        System.out.println(receiversStr);
    }

    public static String replaceCommon(String aimStr, String key, String regularExpr, String replaceExpr) {
        return aimStr.replaceAll( "\""+key+"\""+":\\s*\""+regularExpr+"\"", "\""+key+"\""+":\""+replaceExpr+"\"");
    }

输出:

java 复制代码
[
	{
		"email": "1****9@qq.com",
		"mobile": "189****0711",
		"orgId": "59",
		"orgName": "组织机构/公司领导部门",
		"regionName": "福建",
		"staffId": "1",
		"staffName": "张**",
		"tel": "18920220711,18955462232"
	},
	{
		"email": "1****8@qq.com",
		"mobile": "189****8733",
		"orgId": "59",
		"orgName": "组织机构/公司领导部门",
		"regionName": "福建",
		"staffId": "2",
		"staffName": "L**",
		"tel": "18920228733,18955462252"
	}
]

把tel的配置enabled改成true再执行,输出:

java 复制代码
============ mask ============ 
[
	{
		"email": "1****9@qq.com",
		"mobile": "189****0711",
		"orgId": "59",
		"orgName": "组织机构/公司领导部门",
		"regionName": "福建",
		"staffId": "1",
		"staffName": "张**",
		"tel": "189****0711,189****2232"
	},
	{
		"email": "1****8@qq.com",
		"mobile": "189****8733",
		"orgId": "59",
		"orgName": "组织机构/公司领导部门",
		"regionName": "福建",
		"staffId": "2",
		"staffName": "L**",
		"tel": "189****8733,189****2252"
	}
]

可以看到人名(staffName)和电话号码(mobile),都进行了脱敏。 tel进行设置后,也脱敏了。达到想要的效果

总结:

对数据进行脱敏,可以考虑根据配置规则,这样可以动态修改规则,指定字段内容进行脱敏,会比较方便。 也可以考虑用正则表达式

java 使用正则进行匹配人名电话

相关推荐
公贵买其鹿5 分钟前
List深拷贝后,数据还是被串改
java
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫5 小时前
泛型(2)
java
超爱吃士力架5 小时前
邀请逻辑
java·linux·后端
南宫生5 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石5 小时前
12/21java基础
java
李小白665 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp5 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea