Java日志手机号脱敏工具类
背景
在开发过程中,很容易将用户敏感信息,例如手机号码、身份证等,打印在日志平台。为了保护用户数据,又不影响日志的打印,需要将日志中的敏感信息进行脱敏。
效果
强烈建议 pull项目,执行一下项目中
SensitiveUtils#main
方法。
特性
- 支持多层级【Json】/【对象】字段脱敏
- 支持一次多字段脱敏
- 支持除【连续数组层次(下面会举例)】脱敏
- 不侵入业务代码(例如使用注解进行脱敏)
使用
-
输入为字符串/对象及单Json路径
// 传入对象 User user = new User(); user.setName("xiaoming"); user.setPhone("13455556666"); String strResult4 = SensitiveUtils.desMobilePhone(user, "phone"); System.out.println(strResult4); // {"phone":"134****6666","name":"xiaoming"} // 传入json字符串 String str1 = "{\"name\":\"xiaoming\",\"phone\":\"13455556666\"}"; String strResult5 = SensitiveUtils.desMobilePhone(str1, "phone"); System.out.println(strResult5); // {"phone":"134****6666","name":"xiaoming"}
-
输入为字符串/对象及多Json路径
上图中,如果要脱敏全部手机号,路径则为 :
phone
,parent#phone
String str8 = "[{\"name\":\"xiaoliu\",\"phone\":\"13522222222\",\"parent\":[{\"name\":\"oldliu\",\"phone\":\"13533333333\"}]},{\"name\":\"xiaowang\",\"phone\":\"13500000000\",\"parent\":[{\"name\":\"oldwang\",\"phone\":\"13511111111\"},{\"name\":\"oldzhang\",\"phone\":\"13555555555\"}]}]"; String strResult8 = SensitiveUtils.desMobilePhone(str8, new HashSet<>(Arrays.asList("phone", "parent#phone"))); System.out.println(strResult8); // [{"parent":[{"phone":"135****3333","name":"oldliu"}],"phone":"135****2222","name":"xiaoliu"},{"parent":[{"phone":"135****1111","name":"oldwang"},{"phone":"135****5555","name":"oldzhang"}],"phone":"135****0000","name":"xiaowang"}]
上图中,如果要脱敏全部手机号,路径则为 :
phone
,parent#phone
String str9 = "{\"name\":\"xiaowang\",\"phone\":\"13500000000\",\"parent\":{\"name\":\"oldwang\",\"phone\":\"13511111111\"}}"; String strResult9 = SensitiveUtils.desMobilePhone(str9, new HashSet<>(Arrays.asList("phone", "parent#phone"))); System.out.println(strResult9);
已知缺陷
-
暂不支持连续俩层数组结构的JSON字符串/对象
-
暂不支持对String以外类型脱敏
-
暂不支持字符串中【对象JSON字符串】脱敏
{ "info": "{\"data\":\"{\\\"phone\\\":\\\"13444444444\\\"}\"}" }
未来优化方向
- 增加更多脱敏类型(如身份证号码)
- 支持一个对象/Json字符串多种脱敏类型,例如:一个字符串同时脱敏手机号、身份证号
- 连续数组脱敏(待定)
- 支持非String类型字段脱敏(待定)
- 字符串中【对象JSON字符串】脱敏(待定)
Github地址
本文来自博客园,作者:帅气的涛啊,转载请注明原文链接:https://www.cnblogs.com/handsometaoa/p/18578888
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-11-30 2016年蓝桥杯省赛C++A组 消除尾一