在实际开发过程中,我们时常出于隐私的要求,不再返回完整的用户姓名,电话等隐私信息,这时就需要用到数据脱敏技术,而现在已经有很成熟的脱敏技术依赖,不再需要自己开发脱敏实现了。下面就介绍脱敏技术中比较成熟的desensitization技术。
desensitization是基于Java反射api、零依赖、简单易用、支持各种复杂数据结构的数据脱敏库,包含但不限于以下类型的数据脱敏:
下面以springboot项目为例,示例如何使用desensitization实现数据脱敏。
<!-- 数据脱敏-->
<dependency>
<groupId>red.zyc</groupId>
<artifactId>desensitization</artifactId>
<version>2.4.3</version>
</dependency>
在对象上加上脱敏注解:
public class Teacher {
private Integer id;
@ChineseNameSensitive
private String name;
@IdCardNumberSensitive
private String idCard;
@PhoneNumberSensitive
private String tel;
@EmailSensitive
private String email;
@PasswordSensitive
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Teacher() {
}
public Teacher(Integer id, String name, String idCard, String tel, String email, String password) {
this.id = id;
this.name = name;
this.idCard = idCard;
this.tel = tel;
this.email = email;
this.password = password;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", idCard='" + idCard + '\'' +
", tel='" + tel + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
返回对象前用Sensitive.desensitize实现脱敏:
@GetMapping("/get")
public String get(){
Teacher teacher = Sensitive.desensitize(new Teacher(1,"初夏老师","400000000000000000","15900000000","[email protected]","123456789"));
return teacher.toString();
}
返回示例如下:
Teacher{id=1, name='初***', idCard='400000********0000', tel='159****0000', email='1*********@qq.com', password='*********'}
可以看到对象中标了注解的属性分别进行了脱敏处理后返回。
可能你的敏感信息是一个字符串类型的值或者是一个Collection、Array、Map之类的值,同样擦除它们的敏感信息也很简单。
public static void main(String[] args) {
// String
System.out.printf("字符串脱敏: %s%n", Sensitive.desensitize("[email protected]", new TypeToken<@EmailSensitive String>() {
}));
// Collection
System.out.printf("集合脱敏: %s%n", Sensitive.desensitize(Stream.of("[email protected]", "[email protected]", "[email protected]").collect(Collectors.toList()),
new TypeToken<List<@EmailSensitive String>>() {
}));
// Array
System.out.printf("数组脱敏: %s%n", Arrays.toString(Sensitive.desensitize(new String[]{"[email protected]", "[email protected]", "[email protected]"},
new TypeToken<@EmailSensitive String[]>() {
})));
// Map
System.out.printf("Map脱敏: %s%n", Sensitive.desensitize(Stream.of("张三", "李四", "小明").collect(Collectors.toMap(s -> s, s -> "[email protected]")),
new TypeToken<Map<@ChineseNameSensitive String, @EmailSensitive String>>() {
}));
}
运行结果如下:
字符串脱敏: 1*****@qq.com
集合脱敏: [1*****@qq.com, 1******@qq.com, 1******@qq.com]
数组脱敏: [1*****@qq.com, 1******@qq.com, 1*******@qq.com]
Map脱敏: {小*=1*****@qq.com, 李*=1*****@qq.com, 张*=1*****@qq.com}
注意:
在上面的例子中通过TypeToken构造需要脱敏对象的类型以便我们能够准确的捕获被脱敏对象的实际类型和相应的敏感注解。 这里有一个很重要的地方需要我们格外的关注:由于jdk在解析注解时的bug导致无法正确的获取嵌套类上的注解,TypeToken必须在静态方法、静态代码块中初始化或者作为静态变量初始化,不能在实例方法、实例代码块中初始化同时也不能作为成员变量初始化,这样运行时才能正确的获取脱敏对象上的注解。