java注解式脱敏

java注解式脱敏

  • 简述
  • 代码讲解
    • 脱敏枚举类
    • 注解类编写
    • 脱敏工具类
    • 脱敏AOP的实现
  • 实战演练
  • 友情链接

简述

随着互联网时代普及,用户的信息越来越重要,我们开发软件过程中也需要对用户的信息进行脱敏处理活着加密处理,针对于比较繁杂的工作,个人来讲解如何实现注解式脱敏,支持静态调用和aop统一拦截实现脱敏或者加密返回。

代码讲解

脱敏枚举类

    定义枚举类,处理所有脱敏和加密等,同时可扩展性,这里只是注解式调用方法而已,以便编写样例。DesensitizationEnum若还需要其他脱敏或者加密方法是,只需要添加下面枚举类型即可

package com.lgh.common.sensitive;


import com.lgh.common.utils.MaskUtils;

import java.lang.reflect.Method;

/**
 * 若需要新定义一个扫描规则,这里添加即可
 *
 * @author lgh
 * @version 1.0
 * @date 2021/1/17
 */
public enum DesensitizationEnum {
     

    // 执行类和脱敏方法名
    PHONE(MaskUtils.class, "maskPhone", new Class[]{
     String.class});

     private Class<?> clazz;
     private Method method;

    DesensitizationEnum(Class<?> target, String method, Class[] paramTypes) {
     
        this.clazz = target;
        try {
     
            this.method = target.getDeclaredMethod(method, paramTypes);
        } catch (NoSuchMethodException e) {
     
             e.printStackTrace();
        }
    }
    public Method getMethod() {
     
        return method;
    }
}

脱敏工具

package com.lgh.common.utils;

import org.springframework.util.StringUtils;

/**
 * @author lgh
 * @version 1.0
 * @date 2021/1/17
 */
public class MaskUtils {
     

    public static String maskPhone(String phone){
     
        if(StringUtils.isEmpty(phone) || phone.length() < 8){
     
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
    }
}

注解类编写

此类添加到需要脱敏的类属性上即可实现脱敏,具体是递归遍历此注解,通过反射机制来实现脱敏功能

package com.lgh.common.sensitive;

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

/**
 * 参数定义注解类
 * @author linguohu
 * @version 1.0
 * @date 2021/1/17
 **/
@Target({
     ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveValid {
     
    DesensitizationEnum type();
}

脱敏工具类

特殊声明,我们递归时是索引递归,会出现死循环的情况,比如对象引用了对象,循环地址引用,所以会出现死循环,这里设置了10层递归,一般我们也不允许有那么深的对象设置。

package com.lgh.common.utils;

import com.lgh.common.sensitive.DesensitizationEnum;
import com.lgh.common.sensitive.SensitiveValid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

/**
 * 对象脱敏工具
 *
 * @author lgh
 * @version 1.0
 * @date 2021/1/17
 */
public class DesensitizationUtils {
     
    private static final Logger log = LoggerFactory.getLogger(DesensitizationUtils.class);

    private DesensitizationUtils() {
     
    }

    /**
     * 扫描对象注解,脱敏,最高层次8层
     *
     * @param obj
     */
    public static void format(Object obj) {
     
        DesensitizationUtils.formatMethod(obj, 10);
    }

    /**
     * 递归遍历数据,因为可能有对象地址应用导致循环问题,同时设置莫名奇妙的异常,所以设置递归层次,一般不要超过10层
     *
     * @param obj   需要反射对象
     * @param level 递归层次,必须输入
     */
    private static void formatMethod(Object obj, int level) {
     
        if (obj == null || isPrimitive(obj.getClass()) || level <= 0) {
     
            return;
        }
        if (obj.getClass().isArray()) {
     
            for (Object object : (Object[]) obj) {
     
                formatMethod(object, level--);
            }
        } else if (Collection.class.isAssignableFrom(obj.getClass())) {
     
            for (Object o : ((Collection) obj)) {
     
                formatMethod(o, level--);
            }
        } else if (Map.class.isAssignableFrom(obj.getClass())) {
     
            for (Object o : ((Map) obj).values()) {
     
                formatMethod(o, level--);
            }
        } else {
     
            objFormat(obj, level);
        }
    }

    /**
     * 只有对象才格式化数据
     *
     * @param obj
     * @param level
     */
    private static void objFormat(Object obj, int level) {
     
        for (Field field : obj.getClass().getDeclaredFields()) {
     
            try {
     
                if (isPrimitive(field.getType())) {
     
                    SensitiveValid sensitiveValid = field.getAnnotation(SensitiveValid.class);
                    if (sensitiveValid != null) {
     
                        ReflectionUtils.makeAccessible(field);
                        DesensitizationEnum desensitizationEnum = sensitiveValid.type();
                        Object fieldV = desensitizationEnum.getMethod().invoke(null, field.get(obj));
                        ReflectionUtils.setField(field, obj, fieldV);
                    }
                } else {
     
                    ReflectionUtils.makeAccessible(field);
                    Object fieldValue = ReflectionUtils.getField(field, obj);
                    if (fieldValue == null) {
     
                        continue;
                    }
                    formatMethod(fieldValue, level - 1);
                }
            } catch (Exception e) {
     
                log.error("脱敏数据处理异常", e);
            }
        }
    }

    /**
     * 基本数据类型和String类型判断
     *
     * @param clz
     * @return
     */
    public static boolean isPrimitive(Class<?> clz) {
     
        try {
     
            if (String.class.isAssignableFrom(clz) || clz.isPrimitive()) {
     
                return true;
            } else {
     
                return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
            }
        } catch (Exception e) {
     
            return false;
        }
    }
}

脱敏AOP的实现

aop插拔式编程,以便防止有不需要的操作,所以编写可控制类注解EnableDesensitization

package com.lgh.common.sensitive;

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

/**
 * 方法返回值拦截器,需要注解才生效
 * @author lgh
 * @version 1.0
 * @date 2021/1/17
 **/
@Target({
     ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableDesensitization {
     
}

最后实现拦截aop

package com.lgh.common.sensitive;

import com.lgh.common.utils.DesensitizationUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.aspectj.lang.annotation.Aspect;
import java.lang.reflect.Method;

/**
 * @author lgh
 * @version 1.0
 * @date 2021/1/17
 */
@Aspect
@Configuration
public class SensitiveAspect {
     
    public static final String ACCESS_EXECUTION = "execution(* com.lgh.controller..*.*(..))";
    /**
     * 注解脱敏处理
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(ACCESS_EXECUTION)
    public Object sensitiveClass(ProceedingJoinPoint joinPoint) throws Throwable {
     
        return sensitiveFormat(joinPoint);
    }

    /**
     * 插拔式注解统一拦截器。@{link EnableDesensitization } 和 @SensitiveValid
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    public Object sensitiveFormat(ProceedingJoinPoint joinPoint) throws Throwable {
     
        Object obj = joinPoint.proceed();
        if (obj == null || DesensitizationUtils.isPrimitive(obj.getClass())) {
     
            return obj;
        }
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        EnableDesensitization desensitization = joinPoint.getTarget().getClass().getAnnotation(EnableDesensitization.class);
        if (desensitization != null || method.getAnnotation(EnableDesensitization.class) != null) {
     
            DesensitizationUtils.format(obj);
        }
        return obj;
    }
}

实战演练

我居于上一章节的UserDetail对象增加phone字段,同时加入注解,如下:

package com.lgh.common.authority.entity;

import com.lgh.common.sensitive.DesensitizationEnum;
import com.lgh.common.sensitive.SensitiveValid;

public class UserDetail {
     
    private long id;
    private String name;
    @SensitiveValid(type = DesensitizationEnum.PHONE)
    private String phone;

    public long getId() {
     
        return id;
    }

    public void setId(long id) {
     
        this.id = id;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public void setPhone(String phone) {
     
        this.phone = phone;
    }

    public String getPhone() {
     
        return phone;
    }
}

接下来controller中启动注解

@GetMapping("/detail")
    @EnableDesensitization
    public IResult<UserDetail> getUser(@AuthenticationPrincipal UserDetail userDetail) {
     
        return CommonResult.successData(userDetail);
    }

大功告成,接下来我们实现一下访问操作
在这里插入图片描述

友情链接

github

你可能感兴趣的:(后端,java,java,spring)