Java接口之脱敏工具

Desensitized.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 脱敏注解
 *
 * @author Jalon
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Desensitized {

    DesensitizedStrategy strategy();
}

DesensitizedStrategy.java

import java.util.function.Function;

/**
 * 脱敏策略
 */
public enum DesensitizedStrategy {

    /**
     * 姓名
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
    /**
     * 身份证号
     */
    ID_CARD(s -> s.replaceAll("(\\d{6})\\d{8}(\\w{4})", "$1********$2")),
    /**
     * 手机号
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * 地址
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****"));


    private final Function<String, String> desensitizer;

    DesensitizedStrategy(Function<String, String> desensitizer) {
        this.desensitizer = desensitizer;
    }

    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}

DesensitizedUtils.java

import com.qdbeiqi.bpm.domain.library.configuration.Desensitized;
import com.qdbeiqi.bpm.domain.library.configuration.DesensitizedStrategy;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 脱敏工具
 *
 * @author Jalon
 * @since 2023/6/29 10:19
 **/
public class DesensitizedUtils {

    /**
     * 数组脱敏
     */
    public static <T> void desensitizeList(List<T> objects, Class<T> clazz) throws IllegalAccessException {
        for (T object : objects) {
            desensitize(object, clazz);
        }
    }

    /**
     * 对象脱敏
     */
    public static <T> void desensitize(T object, Class<T> clazz) throws IllegalAccessException {
        Objects.requireNonNull(object, "muset be not null!");
        //此处拿到对象,应该遍历对象所有变量的注解,并使用注解名称获取Map中对应的策略进行数据转换
        List<Field> allField = getFieldByCurrentAndSuper(clazz);
        if (allField.size() > 0) {
            for (Field field : allField) {
                // 设置可访问私有属性
                field.setAccessible(true);

                Desensitized annotation = field.getAnnotation(Desensitized.class);
                if (Objects.nonNull(annotation) && Objects.equals(String.class, field.getType())) {
                    DesensitizedStrategy strategy = annotation.strategy();
                    Object o = field.get(object);
                    if (!Objects.isNull(o)) {
                        String s = strategy.desensitizer().apply(String.valueOf(o));
                        field.set(object, s);
                    }
                }
            }
        }
    }

    /**
     * description: 从当前以及父类中获取全部字段
     *
     * @param clazz 属性所在类
     * @return java.lang.reflect.Field
     */
    public static List<Field> getFieldByCurrentAndSuper(Class<?> clazz) {
        List<Field> fields = new ArrayList<>();
        getFieldByCurrentAndSuper(clazz, fields);
        return fields;
    }


    /**
     * description: 从当前以及父类中获取全部字段
     *
     * @param clazz 属性所在类
     * @return java.lang.reflect.Field
     */
    private static List<Field> getFieldByCurrentAndSuper(Class<?> clazz, List<Field> fields) {
        Field[] declaredFields = clazz.getDeclaredFields();
        fields.addAll(Arrays.asList(declaredFields));
        if (!clazz.equals(Object.class)) {
            return getFieldByCurrentAndSuper(clazz.getSuperclass(), fields);
        }
        return fields;
    }
}

使用

List<VisitorInterviewVo> visitorInterviewVos = this.visitorInterviewService.queryByPage(interviewSearch);
DesensitizedUtils.desensitizeList(visitorInterviewVos, VisitorInterviewVo.class);
return ResponseResult.success(PageInfo.of(visitorInterviewVos));

你可能感兴趣的:(Java,java,开发语言,脱敏工具)