signed

QiShunwang

“诚信为本、客户至上”

用户信息(密码、手机号)加密、解密。---------- 经营开发平台

2021/6/3 15:15:48   来源:

1.在工具包下加入加密、解密类
加密

/**
 * 对敏感信息例如身份证、手机号进行加密
 *
 * @Author: qinencheng
 * @Date: 2021/04/07
 */
public class SensitiveInfoDeserializer extends JsonDeserializer {

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {
        String plainSensitiveInfo = jsonParser.getText();
        try {
            String cipherSensitiveInfo = SensitiveInfoUtil.encryptSensitiveInfoBak(plainSensitiveInfo);
            return cipherSensitiveInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

在这里插入代码片

解密

/** 对敏感信息例如身份证、手机号进行解密
 * @Author: qinencheng
 * @Date: 20210407
 */
public class SensitiveInfoSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        String cipherSensitiveInfo = String.valueOf(value);
        try {
            String plainSensitiveInfo = SensitiveInfoUtil.decryptSensitiveInfoBak(cipherSensitiveInfo);
            gen.writeString(plainSensitiveInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.使用注解序列化,反序列化。

  /**
     * 手机号
     */
    @TableField(value = "TEL")
    @ApiModelProperty(value = "手机号")
    @JsonDeserialize(using = SensitiveInfoDeserializer.class)
    @JsonSerialize(using = SensitiveInfoSerializer.class)
    private String tel;