利用注解进行数据脱敏

创建一个注解:

import java.lang.annotation.*;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CoverPhone {
}

工具类

import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;

import java.lang.reflect.Field;
import java.util.List;

public class DesensitizationUtils {

    private static final String REGEX_CAR = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{3,4}[A-Z0-9挂学警港澳]{1}$";
    private static final Pattern PATTERN_REGEX_CAR = Pattern.compile(REGEX_CAR);

    public static Boolean isCarPlate(String plate) {
        return isMatch(PATTERN_REGEX_CAR, plate);
    }


    private static final String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
    private static final Pattern PATTERN_REGEX_PHONE = Pattern.compile(REGEX_PHONE);


    public static boolean isPhone(String mobile) {
        return isMatch(PATTERN_REGEX_PHONE, mobile);
    }


    private static final String REGEX_IDCARD = "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
    private static final Pattern PATTERN_REGEX_IDCARD = Pattern.compile(REGEX_IDCARD);


    public static boolean isIdCard(String idCard)   {
        return isMatch(PATTERN_REGEX_IDCARD, idCard);
    }






    public static void listDesensitization(List list) {
        for (Object object : list){
            objectDesensitization(object);
        }
    }


    public static void objectDesensitization(Object source) {
        Class itemClass = source.getClass();
        Field[] declaredFields = itemClass.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            coverPhone(field,source);
          
        }
    }

    @SneakyThrows
    private static void coverPhone(Field field, Object source){
        CoverPhone coverPhone = field.getDeclaredAnnotation(CoverPhone.class);
        if (ObjectUtils.isEmpty(coverPhone)){
            return ;
        }
        Object value = field.get(source);
        if (ObjectUtils.isEmpty(value)) {
            return ;
        }
        if (value instanceof String){
            if(((String) value).length() ==11) {
                field.set(source, ((String) value).replaceAll("(\\d{1})\\d{6}(\\d{4})", "$1****$2"));
            }else{
                int len  =  ((String) value).length();
                String startStr = ((String) value).substring(0, 4);
                String endStr = ((String) value).substring(len - 4);
                field.set(source, startStr + "*****"+endStr);
            }
        }
    }
}

测试类

 @Data
    class User{
 
        private String name;

   
        private String idCard;

        @CoverPhone
        private String phone;


        private String plate;

    }



    @SneakyThrows
    @Test
    public void demo2() {
        User user = new User();
        user.setName("张三");
        user.setIdCard("320683198512093154");
        user.setPlate("浙BF3270");
        user.setPhone("18069861630");

        User user1 = new User();
        user1.setName("张三李四王二");
        user1.setIdCard("320683198512093154");
        user1.setPlate("浙BF32708");
        user1.setPhone("0574-65856006");



        ArrayList arrayList = new ArrayList<>();
        arrayList.add(user);
        arrayList.add(user1);

        DesensitizationUtils.listDesensitization(arrayList);
        System.out.println(JSONObject.toJSONString(arrayList));
    }


你可能感兴趣的:(java)