代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
敏感信息屏蔽工具
作者:
文滔
/ 发布于
2016/12/6
/
830
package com.ucf.platform.framework.core.util; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.ucf.platform.framework.core.annotation.SensitiveInfo; import com.ucf.platform.framework.core.log.UcfLogger; import com.ucf.platform.framework.core.log.UcfLoggerFactory; /** * @Title: SensitiveInfoUtils.java * @Copyright: Copyright (c) 2011 * @Description: * 敏感信息屏蔽工具 * @Company: ucfgroup.com * @Created on 2014-11-18 下午3:50:15 * @author liuwentao@ucfgroup.com */ public final class SensitiveInfoUtils { private final static UcfLogger logger = UcfLoggerFactory.getLogger(SensitiveInfoUtils.class); /** * [中文姓名] 只显示第一个汉字,其他隐藏为2个星号<例子:李**> * * @param name * @return */ public static String chineseName(String fullName) { if (StringUtils.isBlank(fullName)) { return ""; } String name = StringUtils.left(fullName, 1); return StringUtils.rightPad(name, StringUtils.length(fullName), "*"); } /** * [中文姓名] 只显示第一个汉字,其他隐藏为2个星号<例子:李**> * * @param familyName * @param givenName * @return */ public static String chineseName(String familyName, String givenName) { if (StringUtils.isBlank(familyName) || StringUtils.isBlank(givenName)) { return ""; } return chineseName(familyName + givenName); } /** * [身份证号] 显示最后四位,其他隐藏。共计18位或者15位。<例子:*************5762> * * @param id * @return */ public static String idCardNum(String id) { if (StringUtils.isBlank(id)) { return ""; } String num = StringUtils.right(id, 4); return StringUtils.leftPad(num, StringUtils.length(id), "*"); } /** * [固定电话] 后四位,其他隐藏<例子:****1234> * * @param num * @return */ public static String fixedPhone(String num) { if (StringUtils.isBlank(num)) { return ""; } return StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*"); } /** * [手机号码] 前三位,后四位,其他隐藏<例子:138******1234> * * @param num * @return */ public static String mobilePhone(String num) { if (StringUtils.isBlank(num)) { return ""; } return StringUtils.left(num, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*"), "***")); } /** * [地址] 只显示到地区,不显示详细地址;我们要对个人信息增强保护<例子:北京市海淀区****> * * @param address * @param sensitiveSize * 敏感信息长度 * @return */ public static String address(String address, int sensitiveSize) { if (StringUtils.isBlank(address)) { return ""; } int length = StringUtils.length(address); return StringUtils.rightPad(StringUtils.left(address, length - sensitiveSize), length, "*"); } /** * [电子邮箱] 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示<例子:g**@163.com> * * @param email * @return */ public static String email(String email) { if (StringUtils.isBlank(email)) { return ""; } int index = StringUtils.indexOf(email, "@"); if (index <= 1) return email; else return StringUtils.rightPad(StringUtils.left(email, 1), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email))); } /** * [银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号<例子:6222600**********1234> * * @param cardNum * @return */ public static String bankCard(String cardNum) { if (StringUtils.isBlank(cardNum)) { return ""; } return StringUtils.left(cardNum, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(cardNum, 4), StringUtils.length(cardNum), "*"), "******")); } /** * [公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号<例子:12********> * * @param code * @return */ public static String cnapsCode(String code) { if (StringUtils.isBlank(code)) { return ""; } return StringUtils.rightPad(StringUtils.left(code, 2), StringUtils.length(code), "*"); } /** * [银行卡有效期] 前1位,后1位,其他隐藏<例子:“0**6”> * * @param num * @return */ public static String cardValidDate(String date) { if (StringUtils.isBlank(date)) { return ""; } return StringUtils.left(date, 1).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(date, 1), StringUtils.length(date), "*"), "*")); } /** * 全部隐藏 * * @param num * @return */ public static String all(String date) { if (StringUtils.isBlank(date)) { return ""; } return StringUtils.repeat("*", StringUtils.length(date)); } /** * 获取脱敏json串 <注意:递归引用会导致java.lang.StackOverflowError> * * @param javaBean * @return */ public static String getJson(Object javaBean) { String json = null; if (null != javaBean) { Class<? extends Object> raw = javaBean.getClass(); try { Object clone = UcfSerializer.deSerialize(UcfSerializer.serialize(javaBean)); Set<Integer> referenceCounter = new HashSet<Integer>(); if (raw.isArray() && !raw.getComponentType().isPrimitive()) { int len = Array.getLength(clone); for (int i = 0; i < len; i++) { Object arrayObject = Array.get(clone, i); SensitiveInfoUtils.replace(arrayObject, referenceCounter); } } else if (clone instanceof Collection<?>) { Collection<?> c = (Collection<?>) clone; Iterator<?> it = c.iterator(); while (it.hasNext()) { Object collectionObj = it.next(); Class<? extends Object> collectionObjClass = collectionObj.getClass(); if (collectionObjClass.isPrimitive() || String.class.equals(collectionObjClass)) break; SensitiveInfoUtils.replace(collectionObj, referenceCounter); } } else if (clone instanceof Map<?, ?>) { Collection<?> c = (Collection<?>) clone; Iterator<?> it = c.iterator(); while (it.hasNext()) { Object collectionObj = it.next(); Class<? extends Object> collectionObjClass = collectionObj.getClass(); if (collectionObjClass.isPrimitive() || String.class.equals(collectionObjClass)) break; SensitiveInfoUtils.replace(collectionObj, referenceCounter); } } else { SensitiveInfoUtils.replace(clone, referenceCounter); } json = JSON.toJSONString(clone, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty); referenceCounter.clear(); referenceCounter = null; } catch (Throwable e) { logger.error("SensitiveInfoUtils.getJson() ERROR", e); } } return json; } private static Field[] findAllField(Class<?> clazz) { Field[] fileds = clazz.getDeclaredFields(); while (null != clazz.getSuperclass() && !Object.class.equals(clazz.getSuperclass())) { fileds = (Field[]) ArrayUtils.addAll(fileds, clazz.getSuperclass().getDeclaredFields()); clazz = clazz.getSuperclass(); } return fileds; } private static void replace(Object javaBean, Set<Integer> referenceCounter) throws IllegalArgumentException, IllegalAccessException { Class<?> clazz = null; if (null != javaBean && null != (clazz = javaBean.getClass()) && referenceCounter.add(javaBean.hashCode())) { Field[] fields = SensitiveInfoUtils.findAllField(clazz); if (null != fields && fields.length > 0) { for (Field field : fields) { field.setAccessible(true); if (null != field) { Object value = field.get(javaBean); if (null != value) { Class<?> type = value.getClass(); // 1.处理子属性,包括集合中的 if (type.isArray() && !type.getComponentType().isPrimitive()) { int len = Array.getLength(value); for (int i = 0; i < len; i++) { Object arrayObject = Array.get(value, i); SensitiveInfoUtils.replace(arrayObject, referenceCounter); } } else if (value instanceof Collection<?>) { Collection<?> c = (Collection<?>) value; Iterator<?> it = c.iterator(); while (it.hasNext()) { Object collectionObj = it.next(); Class<? extends Object> collectionObjClass = collectionObj.getClass(); if (collectionObjClass.isPrimitive() || String.class.equals(collectionObjClass)) break; SensitiveInfoUtils.replace(collectionObj, referenceCounter); } } else if (value instanceof Map<?, ?>) { Map<?, ?> m = (Map<?, ?>) value; Set<?> set = m.entrySet(); for (Object o : set) { Entry<?, ?> entry = (Entry<?, ?>) o; Object mapVal = entry.getValue(); SensitiveInfoUtils.replace(mapVal, referenceCounter); } } else if (!type.isPrimitive() && null != type.getPackage() && !StringUtils.startsWith(type.getPackage().getName(), "javax.") && !StringUtils.startsWith(type.getPackage().getName(), "java.") && !StringUtils.startsWith(field.getType().getName(), "javax.") && !StringUtils.startsWith(field.getName(), "java.")) { SensitiveInfoUtils.replace(value, referenceCounter); } } // 2. 处理自身的属性 SensitiveInfo annotation = field.getAnnotation(SensitiveInfo.class); if (null != value && null != annotation) { switch (annotation.type()) { case CHINESE_NAME: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.chineseName((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.chineseName((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case ID_CARD: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.idCardNum((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.idCardNum((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case FIXED_PHONE: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.fixedPhone((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.fixedPhone((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case MOBILE_PHONE: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.mobilePhone((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.mobilePhone((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case ADDRESS: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.address((String) value, 4)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.address((String) v, 4)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case EMAIL: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.email((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.email((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case BANK_CARD: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.bankCard((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.bankCard((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case CNAPS_CODE: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.cnapsCode((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.cnapsCode((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case BANK_CARD_DATE: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.cardValidDate((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.cardValidDate((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } case ALL: { if (field.getType().equals(String.class)) { field.set(javaBean, SensitiveInfoUtils.all((String) value)); } else if (value instanceof Collection<?>) { Collection<Object> valueCollection = (Collection<Object>) value; if (!valueCollection.isEmpty()) { Object[] vlist = valueCollection.toArray(); for (Object v : vlist) { if (v instanceof String) { valueCollection.remove(v); valueCollection.add(SensitiveInfoUtils.all((String) v)); } } field.set(javaBean, valueCollection); } } else { continue; } break; } } } } } } } } public static enum SensitiveType { /** * 中文名 */ CHINESE_NAME, /** * 身份证号 */ ID_CARD, /** * 座机号 */ FIXED_PHONE, /** * 手机号 */ MOBILE_PHONE, /** * 地址 */ ADDRESS, /** * 电子邮件 */ EMAIL, /** * 银行卡 */ BANK_CARD, /** * 公司开户银行联号 */ CNAPS_CODE, /** * 银行卡有效期 */ BANK_CARD_DATE, /** * 全部隐藏 */ ALL; } }
试试其它关键字
敏感信息屏蔽工具
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
文滔
贡献的其它代码
(
10
)
.
查找目录下指定文件名的文件
.
从string中获取date
.
敏感信息屏蔽工具
.
检查上传图片格式和是否包含中文名
.
返回去掉l头两个元素的新列表
.
读取URL内容
.
点击选择文本
.
鼠标悬停时为其他标签设置样式
.
async的封装
.
Jquery 文字高亮
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3