Bootstrap

Json脱敏

前言

博主github
博主个人博客http://blog.healerjean.com

这里有两种脱敏方式,一种是注解脱敏,另一种是字段名匹配脱敏

1、注解脱敏

1.1、注解
/**
 * @author HealerJean
 * @version 1.0v
 * @ClassName SensitiveInfo
 * @date 2019/6/13  20:01.
 * @Description Json序列化脱敏注解
 * 不建议使用注解,建议使用JsonUtils工具类进行脱敏,因为注解会让我们需要必要时输出到前台的信息变的脱敏
 */

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveInfoSerialize.class)
public @interface SensitiveInfo {
   

    SensitiveTypeEnum value() ;

}
1.2、脱敏的类型枚举
package com.hlj.proj.utils.sensitivity;



/**
 * @author HealerJean
 * @version 1.0v
 * @ClassName SensitiveTypeEnum
 * @date 2019/6/13  20:01.
 * @Description 敏感信息枚举类型
 */
public enum SensitiveTypeEnum {
   


    /**
     * 身份证号
     */
    ID_CARD,
    /**
     * 密码
     */
    PASSWORD,
    /**
     * 手机号
     */
    MOBILE_PHONE,
    /**
     * 电子邮件
     */
    EMAIL,
    /**
     * 真实姓名
     */
    NAME,
    /**
     * 账户信息
     */
    ACCOUNT_NO;



}

1.3、Json脱敏序列化
package com.hlj.proj.utils.sensitivity;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;

import java.io.IOException;
import java.util.Objects;

/**
 * @author HealerJean
 * @version 1.0v
 * @ClassName SensitiveInfoSerialize
 * @date 2019/6/13  20:01.
 * @Description Json脱敏序列化
 */
public class SensitiveInfoSerialize extends JsonSerializer<Object> implements ContextualSerializer {
   

    private SensitiveTypeEnum type;

    public SensitiveInfoSerialize() {
   
    }

    public SensitiveInfoSerialize(final SensitiveTypeEnum type) {
   
        this.type = type;
    }


    @Override
    public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
   
        switch (this.type) {
   
            case ID_CARD: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.idCard(String.valueOf(value)));
                break;
            }
            case MOBILE_PHONE: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.mobilePhone(String.valueOf(value)));
                break;
            }
            case EMAIL: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.email(String.valueOf(value)));
                break;
            }
            case ACCOUNT_NO: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.acctNo(String.valueOf(value)));
                break;
            }
            case PASSWORD: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.password(String.valueOf(value)));
                break;
            }
            case NAME: {
   
                jsonGenerator.writeString(SensitiveInfoUtils.realName(String.valueOf(value)));
                break;
            }
            default:
                jsonGenerator.writeString(String.valueOf(value));

        }

    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
   

        if (beanProperty != null) {
   

            // 非 String 类直接跳过
            if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
   
                SensitiveInfo sensitiveInfo = beanProperty.getAnnotation(SensitiveInfo.class);
                if (sensitiveInfo == null) {
   
                    sensitiveInfo = beanProperty.getContextAnnotation(SensitiveInfo.class);
                }
                // 如果能得到注解,就将注解的 value 传入 SensitiveInfoSerialize
                if (sensitiveInfo != null) {
   

                    return new SensitiveInfoSerialize(sensitiveInfo.value());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(beanProperty);

    }
}

1.4、脱敏工具类
package com.hlj.proj.utils.sensitivity;

import org.apache.commons.lang3.StringUtils;


/**
 * @author HealerJean
 * @version 1.0v
 * @ClassName SensitiveInfoUtils
 * @date 2019/6/13  20:01.
 * @Description 脱敏工具类
 */
public class SensitiveInfoUtils {
   

    /**
     * [真实姓名] 显示最后四位,其他隐藏。共计18位或者15位。<例子:*************5762>
     */
    public static String realName(final String realName) {
   
        if (StringUtils
;