java—Mobile phone number desensitization

Mobile phone number desensitization in Java refers to replacing part of the numbers in the real mobile phone number with specific characters to protect the user's personal information. Usually, mobile phone number desensitization will replace the middle or last few digits of the mobile phone number with specific characters, such as "*", "X" or other randomly selected characters.

The purpose of desensitizing the mobile phone number is to prevent the leakage of the user's personal information. In Internet applications, it is necessary to obtain the user's mobile phone number in many scenarios, such as registration, login, password retrieval and other operations, all of which require the input of the mobile phone number for verification. If the application does not desensitize or take other protective measures for the user's mobile phone number, there is a risk of leaking the user's mobile phone number, which may lead to the loss of the user's personal rights.

Using Java to implement mobile phone number desensitization can be flexibly applied to various application scenarios and requirements, and the specific implementation method can be selected according to the actual situation.

Mobile phone number desensitization method

string substitution

Through the string replacement method, the mobile phone number desensitization can be realized by replacing the numbers with the median of 3 to 7 with asterisks.

String mobile = "13812345678";
String result = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
System.out.println(result); // 输出:138****5678

StringBuilder method

Using StringBuilder to build a new desensitized phone number can effectively reduce memory usage.

String mobile = "13812345678";
StringBuilder sb = new StringBuilder(mobile);
sb.replace(3, 7, "****");
String result = sb.toString();
System.out.println(result); // 输出:138****5678

commons-lang3

In the Apache Commons Lang3 tool class library, the StringUtils class is provided, and there is a method called hide method to desensitize the mobile phone number.

import org.apache.commons.lang3.StringUtils;

String mobile = "13812345678";
String result = StringUtils.overlay(mobile, "****", 3, 7);
System.out.println(result); // 输出:138****5678

lombok

In the Lombok framework, an annotation @Mask is provided to perform data desensitization operations. Desensitization of certain attributes in entity classes can be achieved by using lombok's @Data annotation and @Mask annotation.

import lombok.Data;
import lombok.experimental.Accessors;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.serializer.JSONSerializer;

@Data
@Accessors(chain = true)
public class User {
    
    
    private Long id;
    private String name;
    @JSONField(serializeUsing = ToStringSerializer.class) // fastjson对Long类型转为String
    @Mask(prefixNoMaskLen=3, suffixNoMaskLen=4, mask = "*")  // 加这个注解
    private Long mobile;
}

Guess you like

Origin blog.csdn.net/l_010/article/details/131324132