All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kg.component.desensitized.JsonDesensitizedUtils Maven / Gradle / Ivy

There is a newer version: 1.1.26
Show newest version
package com.kg.component.desensitized;

import cn.hutool.core.util.DesensitizedUtil;
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 lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

/**
 * @author ziro
 * @date 2023-02-15 15:08:15
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class JsonDesensitizedUtils extends JsonSerializer implements ContextualSerializer {

    /**
     * 脱敏类型
     */
    private DesensitizedType type;

    @Override
    public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        // 为空直接跳过
        if (beanProperty != null) {
            // 非 String 类直接跳过
            if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
                JsonDesensitized jsonDesensitized = beanProperty.getAnnotation(JsonDesensitized.class);
                if (jsonDesensitized == null) {
                    jsonDesensitized = beanProperty.getContextAnnotation(JsonDesensitized.class);
                }
                if (jsonDesensitized != null) {
                    // 如果能得到注解,就将注解的 value 传入 SensitiveSerialize
                    return new JsonDesensitizedUtils(jsonDesensitized.value());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(beanProperty);
    }

    @Override
    public void serialize(String str, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        switch (this.type) {
            case USER_ID:
                jsonGenerator.writeString(String.valueOf(DesensitizedUtil.userId()));
                break;
            case CHINESE_NAME:
                jsonGenerator.writeString(DesensitizedUtil.chineseName(String.valueOf(str)));
                break;
            case ID_CARD:
                jsonGenerator.writeString(DesensitizedUtil.idCardNum(String.valueOf(str), 6, 4));
                break;
            case FIXED_PHONE:
                jsonGenerator.writeString(DesensitizedUtil.fixedPhone(String.valueOf(str)));
                break;
            case MOBILE_PHONE:
                jsonGenerator.writeString(DesensitizedUtil.mobilePhone(String.valueOf(str)));
                break;
            case ADDRESS:
                jsonGenerator.writeString(DesensitizedUtil.address(String.valueOf(str), 10));
                break;
            case EMAIL:
                jsonGenerator.writeString(DesensitizedUtil.email(String.valueOf(str)));
                break;
            case PASSWORD:
                jsonGenerator.writeString(DesensitizedUtil.password(String.valueOf(str)));
                break;
            case CAR_LICENSE:
                jsonGenerator.writeString(DesensitizedUtil.carLicense(String.valueOf(str)));
                break;
            case BANK_CARD:
                jsonGenerator.writeString(DesensitizedUtil.bankCard(String.valueOf(str)));
                break;
            default:
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy