1. 导入依赖

<!-- hutool 工具包 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.26</version>
</dependency>

2. 自定义注解

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ruoyi.common.enums.DesensitizationType;
import com.ruoyi.common.utils.SensitiveInfoSerializer;

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

/**
 * 自定义脱敏注解
 *
 * @author mars
 */
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveInfoSerializer.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Desensitization {

    DesensitizationType type() default DesensitizationType.DEFAULT;

    /**
     * 前置不需要打码的长度
     */
    int prefixLen() default 0;

    /**
     * 后置不需要打码的长度
     */
    int suffixLen() default 0;

    /**
     * 遮罩字符
     */
    String maskingChar() default "*";
}

3. 自定义枚举类

/**
 * 脱敏类型枚举
 *
 * @author mars
 */
public enum DesensitizationType {

    /**
     * 自定义规则
     */
    CUSTOMIZE_RULE,
    /**
     * 默认的
     */
    DEFAULT,
    /**
     * 用户ID
     */
    USER_ID,
    /**
     * 中文名
     */
    CHINESE_NAME,
    /**
     * 身份证号
     */
    ID_CARD,
    /**
     * 座机号
     */
    FIXED_PHONE,
    /**
     * 手机号
     */
    MOBILE_PHONE,
    /**
     * 地址
     */
    ADDRESS,
    /**
     * 电子邮件
     */
    EMAIL,
    /**
     * 密码
     */
    PASSWORD,
    /**
     * 中国大陆车牌,包含普通车辆、新能源车辆
     */
    CAR_LICENSE,
    /**
     * 银行卡
     */
    BANK_CARD

}

4. 自定义序列化器

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ruoyi.common.annotation.Desensitization;
import com.ruoyi.common.enums.DesensitizationType;

import java.io.IOException;

/**
 * 数据脱敏序列化器
 *
 * @author mars
 */
public class SensitiveInfoSerializer extends JsonSerializer<String> implements ContextualSerializer {

    private boolean useMasking = false;
    private DesensitizationType type;
    private int prefixLen;
    private int suffixLen;
    private String maskingChar;

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (useMasking && value != null) {
            switch (type) {
                case MOBILE_PHONE:
                    gen.writeString(DesensitizedUtil.mobilePhone(value));
                    break;
                case ID_CARD:
                    gen.writeString(DesensitizedUtil.idCardNum(value, prefixLen, suffixLen));
                    break;
                case CUSTOMIZE_RULE:
                    gen.writeString(StrUtil.hide(value, prefixLen, suffixLen));
                    break;
                case CHINESE_NAME:
                    gen.writeString(DesensitizedUtil.chineseName(value));
                    break;
                case EMAIL:
                    gen.writeString(DesensitizedUtil.email(value));
                    break;
                case DEFAULT:
                    gen.writeString(value);
                default:
                    gen.writeString(value);
            }
        } else {
            gen.writeObject(value);
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
        if (property != null) {
            Desensitization desensitization = property.getAnnotation(Desensitization.class);
            if (desensitization != null) {
                this.type = desensitization.type();
                this.prefixLen = desensitization.prefixLen();
                this.suffixLen = desensitization.suffixLen();
                this.maskingChar = desensitization.maskingChar();
                useMasking = true;
            }
        }
        return this;
    }
}