Java数据脱敏

手机号脱敏:

11位手机号中间四位脱敏显示,例如:187****1234

这样处理有2中方法,a:substring来分割  b:正则验证

 public static void main(String[] args) {
        String phone = "13123456789";
        String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        System.out.println("phone总长度:" + phone.length());
        System.out.println("phone前3位:" + phone.substring(0, 3));
        System.out.println("phone中4位:" + phone.substring(3, 7));
        System.out.println("phone后4位:" + phone.substring(7, phone.length()));
        System.out.println("phone中4*:" + phoneNumber);
     }

 

public class DataMaskingUtils{
    //匹配规则
    private static final String REGEX_OF_PHONE = "(^\\d{3})\\d.*(\\d{4}$)";
    //替换规则
    private static final String RRPLAGE_OF_PHONE = "$1****$2";
    
    /**
     * 手机号脱敏 153****8736
     * @param phone
     */
     public static Stirng phoneMasking(String phone){
         if(!StringUtils.isEmpty(phone)){
             return phone.replaceAll(REGEX_OF_PHONE, REPLACE_OF_PHONE);
         }
         return null;
     }
}

身份证号脱敏

public class DataMaskingUtils{
    //匹配规则
    private static final Stirng REGEX_OF_ID="(^\\d{3})\\d.*(\\d{4}$)";
    
    //替换规则
    private static final String REPLACE_OF_ID="$1********$2";
    
    /**
     * 身份证号脱敏 511********9768
     * @param id 身份证号
     */
     public static Stirng idMasking(String id){
         if(!StringUtils.isEmpty(id)){
             return id.replaceAll(REGEX_OF_ID, REPLACE_OF_ID);
         }
         return null;
     }
}

邮箱账号脱敏

public class DataMaskingUtils{
    //匹配规则
    private static final String REGEX_OF_EMAIL="(^\\S)\\S.*(@)";
    
    //替换规则
    private static final String REPLACE_OF_EMAIL="$1***$2";
    
    /**
     * 邮箱账号脱敏 k***@qq.com
     * @param email 邮箱账号
     */
     public static Stirng emailMasking(String email){
         if(!StringUtils.isEmpty(email)){
             return email.replaceAll(REGEX_OF_EMAIL, REPLACE_OF_EMAIL);
         }
         return null;
     }
}

银行卡号脱敏

public class DataMaskingUtils{
    //匹配规则
    private static final String REGEX_OF_CARD="(^\\d{4})\\d.*(\\d{3}$)";
    
    //替换规则
    private static final String REPLCACE_OF_CARD="$1********$2";
    
    /**
     * 银行卡号脱敏 5647********928
     * @param card 银行卡号
     */
     public static Stirng cardMasking(String card){
         if(!StringUtils.isEmpty(card)){
             return card.replaceAll(REGEX_OF_CARD, REPLCACE_OF_CARD);
         }
         return null;
     }
}

姓名脱敏

public class DataMaskingUtils{
    //匹配规则
    private static final String REGEX_OF_NAME="(^\\S\\S.*";
    
    //替换规则
    private static final String REPLACE_OF_NAME="$1**";
    
    /**
     * 姓名脱敏 赵**
     * @param name 姓名
     */
     public static Stirng nameMasking(String name){
         if(!StringUtils.isEmpty(name)){
             return name.replaceAll(REGEX_OF_NAME, REPLACE_OF_NAME);
         }
         return null;
     }
}

姓名脱敏处理,规则:3个字以内脱敏第一个字,4—6个字脱敏前两个字,大于6个字脱敏第3—6个字,示例:张三(*三)、张三水(*三水)、长孙无忌(**无忌)、罗斯托罗司机格(罗斯XXXX格)

    function noPassByName(str){
        if(null != str && str != undefined){
            if(str.length <= 3){
                return "*" + str.substring(1,str.length);
            } else if(str.length > 3 && str.length <= 6){
                return "**" + str.substring(2,str.length);
            } else if(str.length > 6){
                return str.substring(0,2) + "****" + str.substring(6,str.length)
            }
        } else {
            return "";
        }
    }

微信号脱敏

public class DataMaskingUtils{
    //匹配规则
    private static final String REGEX_OF_WECHAT="(^\\S\\S.*";
    
    //替换规则
    private static final String REPLACE_OF_WECHAT="$1***";
    
    /**
     * 微信账号脱敏 k***
     * @param weChat 微信账号
     */
     public static Stirng weChatMasking(String weChat){
         if(!StringUtils.isEmpty(weChat)){
             return weChat.replaceAll(REGEX_OF_WECHAT, REPLACE_OF_WECHAT);
         }
         return null;
     }
}

 

posted @ 2021-10-29 14:30  当当小丸子  阅读(1634)  评论(0编辑  收藏  举报