**敏感信息处理包括:
/*
要考虑到证件号是否为身份证号或者学生证,因为数字位数不同
全部按身份证的方式托面,第三位至最后四位都进行脱敏操作
*/
1.证件号非空时第3位到第14位显示时以*号代替
2.移动电话非空时第2位-第7位显示时以*号代替
3.手机号非空时将中间数字进行脱敏
4.邮箱非空时除前3位其他以*处理**
编码书写
userinfo //用户对象
CertificateNo //证件号
Email //邮箱
Mobile //移动电话
Telephone //电话号
private Userinfo getUserinfoDesensitize(Userinfo userinfo){
//*符号个数
String star = "";
//证件号码
if(userinfo.getCertificateNo()!=null&&!("".equals(userinfo.getCertificateNo()))){
int CertificateNoLength = userinfo.getCertificateNo().length();//获取当前的证件号码的长度
for (int i = 0; i < CertificateNoLength-7; i++) {//获取*的长度,因为前三位和最后四位不需要脱敏,所以长度-7
star += "*";
}
//取最后四位
//总长度-(总长度-(总长度-需要脱敏的位数)-开头不要需要脱敏的长度)
//这个获取的就时末尾不需要脱敏的位数,因为证件号长度不确定,所以需要动态获取
int four = CertificateNoLength - (CertificateNoLength-(CertificateNoLength-7)-3);
userinfo.setCertificateNo(userinfo.getCertificateNo().substring(0,3)+star+userinfo.getCertificateNo().substring(four,CertificateNoLength));
star = "";//最后将符号清空,便于下一个字段使用
}
//邮箱
if(userinfo.getEmail()!=null&&!("".equals(userinfo.getEmail()))){
int emailLength = userinfo.getEmail().length()-3;
for (int i = 0; i < emailLength; i++) {
star += "*";
}
userinfo.setEmail(userinfo.getEmail().substring(0,3)+star);
star = "";
}
//移动电话
if(userinfo.getMobile()!=null&&!("".equals(userinfo.getMobile()))){
//正则表达式 进行脱敏第一位不需要脱敏 中间六位需要脱敏 最后四位不需要脱敏
//中间的*符号我这个是写死的,不管多少位数字,只显示六位 *
userinfo.setMobile(userinfo.getMobile().replaceAll("(\\d{1})\\d{6}(\\w{4})","$1******$2"));
}
//电话号码
if(userinfo.getTelephone()!=null&&!("".equals(userinfo.getTelephone()))){
int telePhoneLength = userinfo.getTelephone().length();
//获取最后三位的位置
int three = telePhoneLength-(telePhoneLength-(telePhoneLength-3)-1);
for (int i = 0; i < telePhoneLength; i++) {
star += "*";
}
userinfo.setTelephone(userinfo.getTelephone().substring(0,3)+star+userinfo.getTelephone().substring(three,telePhoneLength));
star = "";
}
return userinfo;
}