用户信息脱敏
hutool工具包;
定义手机号和证件号的 Jackson 自定义序列化器,并在对应需要脱敏的敏感字段上指定自定义序列化器;
# 手机号码序列化器
public class PhoneDesensitizationSerializer extends JsonSerializer<String> {
@Override
public void serialize(String phone, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String phoneDesensitization = DesensitizedUtil.mobilePhone(phone);
jsonGenerator.writeString(phoneDesensitization);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 身份证号序列化器
public class IdCardDesensitizationSerializer extends JsonSerializer<String> {
@Override
public void serialize(String idCard, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String phoneDesensitization = DesensitizedUtil.idCardNum(idCard, 4, 4);
jsonGenerator.writeString(phoneDesensitization);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 脱敏字段上自定义序列化器
@Data
@Accessors(chain = true)
public class UserRespDTO {
/**
* 证件号码
*/
@JsonSerialize(using = IdCardDesensitizationSerializer.class)
private String idCard;
/**
* 手机号
*/
@JsonSerialize(using = PhoneDesensitizationSerializer.class)
private String phone;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16