一、前言
关于Java在实现涉及用户/客户敏感数据(如手机号、姓名等)打印输出的时候,通常需要将其转换为掩码”*“处理,下面通过具体的源码示例进行说明。
二、示例说明package test;@b@@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@import org.apache.commons.lang.StringUtils;@b@import com.alibaba.fastjson.JSON;@b@import com.alibaba.fastjson.JSONObject;@b@@b@public class CustomerSecretDataFilter {@b@@b@private static Pattern pattern = Pattern.compile("[0-9a-zA-Z]");@b@//手机号掩码字段@b@private static String CUST_PHONE="customerPhone,mobile,MOBILE";@b@//客户姓名掩码字段@b@private static String CUST_NAME="customerName,custName,CUST_NAME,clientName";@b@@b@/**@b@ * 客户手机号,处理日志字符串,返回脱敏后的字符串@b@ * @param msg@b@ * @return@b@ */@b@private static String maskCustPhoneMsg(final String message){ @b@ String msg = handleMsg(CUST_PHONE,message);@b@ return msg;@b@ }@b@ /**@b@ * 客户姓名,处理日志字符串,返回脱敏后的字符串@b@ * @param msg@b@ * @return@b@ */@b@ private static String maskCustNameMsg(final String message){ @b@ String msg = handleMsg(CUST_NAME,message);@b@ return msg;@b@ }@b@ @b@ @b@ /**@b@ * 处理日志字符串,返回脱敏后的字符串@b@ * @param msg@b@ * @return@b@ */@b@ private static String handleMsg(final String handleAttr,final String fullText){@b@ @b@ String msg = new String(fu