提交 | 用户 | age
|
826b66
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import org.apache.commons.lang.StringUtils; |
|
4 |
|
|
5 |
/** |
|
6 |
* 数据脱敏工具类 |
|
7 |
* |
|
8 |
*/ |
|
9 |
public class BlurDataUtil { |
|
10 |
|
|
11 |
/** |
|
12 |
* 手机号脱敏处理 |
|
13 |
* 脱敏规则: 保留前三后四, 比如 18738291234 置换为 187****1234 |
|
14 |
* @param phone |
|
15 |
* @return |
|
16 |
*/ |
|
17 |
public static final String blurPhone(String phone) { |
|
18 |
if (StringUtils.isEmpty(phone) || (phone.length() != 11)) { |
|
19 |
return phone; |
|
20 |
} |
|
21 |
return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* 身份证号脱敏处理 |
|
26 |
* 原身份证号:500222202110275699,脱敏后:132****99308084911 |
|
27 |
* @param idCard |
|
28 |
* @return |
|
29 |
*/ |
|
30 |
public static String blurIdCard(String idCard) { |
|
31 |
if (StringUtils.isEmpty(idCard)) { |
|
32 |
return ""; |
|
33 |
} |
|
34 |
/* |
|
35 |
* 参数1:证件号,参数2(OVERLAY):替换后的字符串, |
|
36 |
* 参数3(START):替换的起始下标,参数4(END):替换的结束下标(不包含) |
|
37 |
*/ |
|
38 |
return StringUtils.overlay(idCard, "****", 3, 7); |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* 身份证号脱敏处理 |
|
43 |
* 展示 前6位和后6位 |
|
44 |
* @param idCard |
|
45 |
* @return |
|
46 |
*/ |
|
47 |
public static String hiddenIdCard(String idCard) {//身份证 |
|
48 |
if (StringUtils.isBlank(idCard)) { |
|
49 |
return ""; |
|
50 |
} |
|
51 |
return StringUtils.left(idCard, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(idCard, 4), StringUtils.length(idCard), "*"), "***")); |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* 邮箱脱敏处理 |
|
56 |
* 原邮箱:zhangsan@qq.com,脱敏后:zhang***@qq.com |
|
57 |
* @param email |
|
58 |
* @return |
|
59 |
*/ |
|
60 |
public static String blurEmail(String email) { |
|
61 |
if (StringUtils.isEmpty(email)) { |
|
62 |
return email; |
|
63 |
} |
|
64 |
String encrypt = email.replaceAll("(\\w+)\\w{3}@(\\w+)", "$1***@$2"); |
|
65 |
if(StringUtils.equalsIgnoreCase(email, encrypt)){ |
|
66 |
encrypt = email.replaceAll("(\\w*)\\w{1}@(\\w+)", "$1*@$2"); |
|
67 |
} |
|
68 |
return encrypt; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 护照脱敏处理 |
|
73 |
* 脱敏规则:护照前2后3位脱敏,护照一般为8或9位 |
|
74 |
* @param passport |
|
75 |
* @return |
|
76 |
*/ |
|
77 |
public static String blurPassport(String passport) { |
|
78 |
if (StringUtils.isEmpty(passport) || (passport.length() < 8)) { |
|
79 |
return passport; |
|
80 |
} |
|
81 |
return passport.substring(0, 2) + new String(new char[passport.length() - 5]).replace("\0", "*") + passport.substring(passport.length() - 3); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* 字段信息脱敏 |
|
86 |
* 脱敏规则:如果字符长度大于3位,则隐藏最后三位,否则隐藏最后1位 |
|
87 |
* @param field |
|
88 |
* @return |
|
89 |
*/ |
|
90 |
public static String blurField(String field) { |
|
91 |
if (StringUtils.isEmpty(field)) { |
|
92 |
return field; |
|
93 |
} |
|
94 |
String encrypt = field.replaceAll("(\\w+)\\w{3}", "$1***"); |
|
95 |
if(StringUtils.equalsIgnoreCase(field, encrypt)){ |
|
96 |
encrypt = field.replaceAll("(\\w*)\\w{1}", "$1*"); |
|
97 |
} |
|
98 |
return encrypt; |
|
99 |
} |
|
100 |
|
|
101 |
public static void main(String[] args) { |
|
102 |
System.out.println(blurPhone("18738291234")); // 187****1234 |
|
103 |
System.out.println(blurIdCard("500222202110275699")); // 500****99410275467 |
|
104 |
System.out.println(blurEmail("zhangsan@qq.com")); // zhang***@qq.com |
|
105 |
System.out.println(blurPassport("12345678")); // 12***678 |
|
106 |
System.out.println(blurField("I feel so good")); // I f*** so g*** |
|
107 |
} |
|
108 |
|
|
109 |
/* |
|
110 |
* 备注: |
|
111 |
* 1、String.replaceAll(第1个参数是脱敏筛选的正则,第2个参数是脱敏替换的正则) |
|
112 |
* 2、需要引入commons-lang3,这个基本每个项目都用到 |
|
113 |
* <dependency> |
|
114 |
* <groupId>org.apache.commons</groupId> |
|
115 |
* <artifactId>commons-lang3</artifactId> |
|
116 |
* <version>3.7</version> |
|
117 |
* </dependency> |
|
118 |
*/ |
|
119 |
} |