自学内容网 自学内容网

java 对人名和电话 脱敏-replaceAll

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

脱敏规则:

DesensitizationRules

@Data
public class DesensitizationRules {
    /**
     *  匹配的字段名称
     */
    public String fieldName;

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

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

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

数据模拟:

    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

@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;
}

模拟数据:

     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,然后对数据进行依次替换脱敏:

     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+"\"");
    }

输出:

[
{
"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再执行,输出:

============ 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 使用正则进行匹配人名电话


原文地址:https://blog.csdn.net/qq_35461948/article/details/143403823

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!