文章目录

  • PatternUtil
  • PrivacyUtil

PatternUtil

package com.yt.eos.common.utils;/*** 正则表达式匹配* @author tyg* @date   2018年9月21日下午4:32:27*/
public class PatternUtil {/** 手机号码匹配 */public static final String PHONE_REG = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";/** 座机号码匹配 */public static final String MOBILE_REG = "^\\d{3}-\\d{8}|\\d{4}-\\d{7}$";/** 汉字匹配 */public static final String CHIINESE_REG = "^[\\u4e00-\\u9fa5]{0,}$";/** 英文和数字匹配(不区分大小写) */public static final String LETTER_NUMBER_REG = "^[A-Za-z0-9]+$";/** 非零开头的最多带两位小数的数字 */public static final String DECIMAL_REG = "^([1-9][0-9]*)+(.[0-9]{1,2})?$";/** 纯数字 */public static final String ONLY_NUMBER_REG = "^[0-9]{1,}$";/** 纯字母 */public static final String ONLY_LETTER_REG = "^[a-zA-Z]{1,}$";/** 邮箱email */public static final String EMAIL_REG = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";/** 日期规则 */public static final String DATE_YMD = "^(\\d{4}-\\d{2}-\\d{2})$";/** 时间规则 */public static final String DATE_YMDHDS = "^(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})$";/** 银行卡卡号位数 */public final static String BANK_CARD_NUMBER = "^\\d{16}|\\d{19}$";/** 身份证号码位数限制 */public final static String ID_CARD = "^\\d{15}|(\\d{17}[0-9,x,X])$";/*** 是否为手机号码* @param phone* @return* @return boolean* @author tyg* @date   2018年9月25日上午9:44:38*/public static boolean isPhone(String phone) {return phone.matches(PHONE_REG);}/*** 是否为座机号码* @param mobile* @return* @return boolean* @author tyg* @date   2018年9月25日上午9:44:38*/public static boolean isMobile(String mobile) {return mobile.matches(MOBILE_REG);}public static void main(String[] args) {System.out.println(isPhone("13228116626"));System.out.println(isMobile("028-63358547"));}
}

PrivacyUtil

package com.yt.eos.common.utils;import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;/*** 日志辅助类,用来处理敏感信息,进行脱敏* @author tyg* @date   2018年5月5日下午4:01:22*/
public class PrivacyUtil {//银行账户:显示前六后四,范例:622848******4568public static String encryptBankAcct(String bankAcct) {if (bankAcct == null) {return "";}return replaceBetween(bankAcct, 6, bankAcct.length() - 4, null);}//身份证号码:显示前六后四,范例:110601********2015public static String encryptIdCard(String idCard) {if (idCard == null) {return "";}return replaceBetween(idCard, 6, idCard.length() - 4, null);}//客户email:显示前二后四,范例:abxx@xxx.compublic static String encryptEmail(String email) {//判断是否为邮箱地址if (email == null || !Pattern.matches(PatternUtil.EMAIL_REG, email)) {return "";}StringBuilder sb = new StringBuilder(email);//邮箱账号名只显示前两位int at_position = sb.indexOf("@");if (at_position > 2) {sb.replace(2, at_position, StringUtils.repeat("x", at_position - 2));}//邮箱域名隐藏int period_position = sb.lastIndexOf(".");sb.replace(at_position + 1, period_position, StringUtils.repeat("x", period_position - at_position - 1));return sb.toString();}//手机:显示前三后四,范例:189****3684public static String encryptPhoneNo(String phoneNo) {if (phoneNo == null) {return "";}return replaceBetween(phoneNo, 3, phoneNo.length() - 4, null);}/*** @param object 待处理对象* @param fieldNames 需要处理的属性* @return * 将对象转换为字符串,并对敏感信息进行处理*/public static String encryptSensitiveInfo(Object object, String[] fieldNames) {return encryptSensitiveInfo(object, fieldNames, null);}/*** @param object 待处理对象* @param fieldNames 需要处理的属性* @param excludes 不需要显示的属性* @return * 将对象转换为字符串,并对敏感信息进行处理*/public static String encryptSensitiveInfo(Object object, String[] fieldNames, String[] excludes) {if (fieldNames == null) {fieldNames = new String[0];}//合并数组Set<String> set = new HashSet<String>(Arrays.asList(fieldNames));if (excludes != null) {for (int i = 0; i < excludes.length; i++) {set.add(excludes[i]);}}//将对象转换为字符串String str = ReflectionToStringBuilder.toStringExclude(object, set.toArray(new String[0]));//处理敏感信息StringBuilder sb = new StringBuilder(str);sb.deleteCharAt(sb.length() - 1);for (int i = 0; i < fieldNames.length; i++) {String fieldName = fieldNames[i];try {Field f = object.getClass().getDeclaredField(fieldName);f.setAccessible(true);String value = encryptSensitiveInfo(String.valueOf(f.get(object)));if (i != 0 || sb.charAt(sb.length() - 1) != '[') {sb.append(",");}sb.append(fieldName).append("=").append(value);} catch (Exception e) {}}sb.append("]");str = sb.toString();return str;}/*** 对敏感信息进行处理。使用正则表达式判断使用哪种规则* @param sourceStr 需要处理的敏感信息* @return* @return String* @author tyg* @date   2018年5月5日下午3:59:28*/public static String encryptSensitiveInfo(String sourceStr) {if (sourceStr == null) {return "";}if (Pattern.matches(PatternUtil.PHONE_REG, sourceStr)) {return encryptPhoneNo(sourceStr);} else if (Pattern.matches(PatternUtil.EMAIL_REG, sourceStr)) {return encryptEmail(sourceStr);} else if (Pattern.matches(PatternUtil.BANK_CARD_NUMBER, sourceStr)) {return encryptBankAcct(sourceStr);} else if (Pattern.matches(PatternUtil.ID_CARD, sourceStr)) {return encryptIdCard(sourceStr);} else {return sourceStr;}}/*** 将字符串开始位置到结束位置之间的字符用指定字符替换* @param sourceStr 待处理字符串* @param begin   开始位置* @param end   结束位置* @param replacement 替换字符* @return */private static String replaceBetween(String sourceStr, int begin, int end, String replacement) {if (sourceStr == null) {return "";}if (replacement == null) {replacement = "*";}int replaceLength = end - begin;if (StringUtils.isNotBlank(sourceStr) && replaceLength > 0) {StringBuilder sb = new StringBuilder(sourceStr);sb.replace(begin, end, StringUtils.repeat(replacement, replaceLength));return sb.toString();} else {return sourceStr;}}public static void main(String[] args) {System.out.println(PrivacyUtil.encryptBankAcct("6228482462893085616"));//622848*********5616System.out.println(PrivacyUtil.encryptPhoneNo("13228116626"));//132****6626System.out.println(PrivacyUtil.encryptIdCard("510658199107356847"));//510658********6847System.out.println(PrivacyUtil.encryptEmail("1182786142@qq.com"));//11xxxxxxxx@xx.comSystem.out.println(PrivacyUtil.encryptSensitiveInfo("13228116626"));//}
}

原文:
https://blog.csdn.net/qq_26365837/article/details/89926395
https://blog.csdn.net/qq_26365837/article/details/89926487

java对银行卡号、手机号码、身份证号码进行脱敏相关推荐

  1. 银行卡四元素校验API 验证姓名手机号码身份证号码银行卡号是否一致

    银行卡四元素校验API,检测输入的姓名.手机号码.身份证号码.银行卡号是否一致.通过https://www.juhe.cn/docs/api/id/213申请APPKEY 1.银行卡四元素检测 接口地 ...

  2. 在java中如何做身份证号码校验

    https://blog.csdn.net/persistencegoing/article/details/84376427 直接上代码,里面有测试类 /*** 身份证前6位[ABCDEF]为行政区 ...

  3. 比较严谨的java验证18位身份证号码

    /** * 我国公民的身份证号码特点如下* 1.长度18位* 2.第1-17号只能为数字* 3.第18位只能是数字或者x* 4.第7-14位表示特有人的年月日信息* 请实现身份证号码合法性判断的函数, ...

  4. java对台湾同胞身份证号码验证

    package com.yt.eos.common.enumclass;import org.apache.commons.lang3.StringUtils;/*** 台湾同胞身份证号码验证* @a ...

  5. 手机号码/身份证号码中间几位的隐藏

    String phone2 = "15988888888"; System.out.println(phone2.substring(0,3) + "****" ...

  6. java验证身份证号码是否有效源代码

    转载自   java验证身份证号码是否有效源代码 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 package test; import java.t ...

  7. Java:15位或18位居民身份证号码通用校验(正则表达式、日期格式、末尾校验码)

    身份证号码校验,正则表达式校验.日期格式校验.18位身份证末尾校验码校验 前六位省市县号码变更频繁,这里就不做校验 import java.text.ParseException; import ja ...

  8. 新旧身份证合法性验证及相互转换算法(三):Java身份证号码验证及将15位转换18位

    package test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 身份证号码验证 * */ pub ...

  9. Java判断身份证号码

    1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

最新文章

  1. 利用SAP FR高效预测客户需求
  2. hadoop: hdfs API示例
  3. Spark Shuffle 解析
  4. 程序员获取编程灵感的 10 种方式
  5. 咋样路linux分区,linux下磁盘分区方法详解
  6. Python3.5以上版本lxml导入etree报错Unresolved reference
  7. jzoj4209-已经没有什么好害怕的了【差分】
  8. python的pillow给图片加文字_Python-Pillow库给图片添加文字、水印
  9. 唯有自己变得强大_真正的自立,唯是让自己变得更加强大
  10. 图像去重,4 行代码就能实现,你值得拥有imagededup
  11. Bootstrap 弹出提示插件popover 的使用方法
  12. 小i机器人伴侣_【数据分析】2020年3月全国工业机器人产量统计数据分析
  13. linux下udp调试工具,linux tcp udp 调试工具
  14. JavaScript学习手册一:JS简介
  15. quot 云计算 quot 是计算机,云计算是什么意思?
  16. gitlab的账号注册以及分组
  17. 【Pyecharts50例】GEO使用外国地图/使用美国地图
  18. php for iis express,iis10.0完整安装包
  19. Java实现简易联网坦克对战小游戏(内涵源码)//Java+Java游戏+拓展学习+资源分享
  20. 面试笔记@MySQL

热门文章

  1. SEO 是什么?SEM是什么?SEO、SEM是做什么的?你必须知道的小知识(扫盲篇)
  2. 两组回归系数差异检验_如何检验两个回归系数的差异性?我做调节分析。
  3. ECharts天气预报折线图
  4. Linux系统代理上网
  5. sipp 注册脚本测试服务端含(401)注册流程(UAC )
  6. directX 正试图在 OS 加载程序锁内执行托管代码
  7. 【一本通】1064:奥运奖牌计数
  8. 微信小程序开发—入门到跑路(一)
  9. 牛顿后插matlab,大神求解析程序~~关于牛顿插值多项式的matlab程序
  10. I/O 的五分钟法则