• java 日志脱敏框架 sensitive-新版本0.0.2-深度拷贝,属性为对象和集合的


    项目介绍

    日志脱敏是常见的安全需求。普通的基于工具类方法的方式,对代码的***性太强。编写起来又特别麻烦。

    本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发。

    用户也可以基于自己的实际需要,自定义注解。

    特性

    1. 基于注解的日志脱敏

    2. 可以自定义策略实现,策略生效条件

    3. 常见的脱敏内置方案

    4. java 深拷贝,且原始对象不用实现任何接口。

    快速开始

    maven 导入

    <dependency>
        <groupId>com.github.houbb</groupId>
        <artifactId>sensitive-core</artifactId>
        <version>0.0.2</version>
    </dependency>
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
     
     

    定义对象

    • User.java

    我们对 password 使用脱敏,指定脱敏策略为 StrategyPassword。(直接返回 null)

    public class User {
    
        @Sensitive(strategy = StrategyChineseName.class)
        private String username;
        
        @Sensitive(strategy = StrategyCardId.class)
        private String idCard;
        
        @Sensitive(strategy = StrategyPassword.class)
        private String password;
        
        @Sensitive(strategy = StrategyEmail.class)
        private String email;
        
        @Sensitive(strategy = StrategyPhone.class)
        private String phone;
        
        //Getter & Setter
        //toString()
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
     
     

    属性为集合或者对象

    如果某个属性是单个集合或者对象,则需要使用注解 @SensitiveEntry

    • 放在集合属性上,且属性为普通对象

    会遍历每一个属性,执行上面的脱敏策略。

    • 放在对象属性上

    会处理对象中各个字段上的脱敏注解信息。

    • 放在集合属性上,且属性为对象

    遍历每一个对象,处理对象中各个字段上的脱敏注解信息。

    放在集合属性上,且属性为普通对象

    • UserEntryBaseType.java

    作为演示,集合中为普通的字符串。

    public class UserEntryBaseType {
    
        @SensitiveEntry
        @Sensitive(strategy = StrategyChineseName.class)
        private List<String> chineseNameList;
    
        @SensitiveEntry
        @Sensitive(strategy = StrategyChineseName.class)
        private String[] chineseNameArray;
        
        //Getter & Setter & toString()
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
     
     
    • 构建对象
    /**
     * 构建用户-属性为列表,列表中为基础属性
     * @return 构建嵌套信息
     * @since 0.0.2
     */
    public static UserEntryBaseType buildUserEntryBaseType() {
        UserEntryBaseType userEntryBaseType = new UserEntryBaseType();
        userEntryBaseType.setChineseNameList(Arrays.asList("盘古", "女娲", "伏羲"));
        userEntryBaseType.setChineseNameArray(new String[]{"盘古", "女娲", "伏羲"});
        return userEntryBaseType;
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
     
     
    • 测试演示
    /**
     * 用户属性中有集合或者map,集合中属性是基础类型-脱敏测试
     * @since 0.0.2
     */
    @Test
    public void sensitiveEntryBaseTypeTest() {
        UserEntryBaseType userEntryBaseType = DataPrepareTest.buildUserEntryBaseType();
        System.out.println("脱敏前原始: " + userEntryBaseType);
        UserEntryBaseType sensitive = SensitiveUtil.desCopy(userEntryBaseType);
        System.out.println("脱敏对象: " + sensitive);
        System.out.println("脱敏后原始: " + userEntryBaseType);
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
     
     
    • 日志信息
    脱敏前原始: UserEntryBaseType{chineseNameList=[盘古, 女娲, 伏羲], chineseNameArray=[盘古, 女娲, 伏羲]}
    脱敏对象: UserEntryBaseType{chineseNameList=[*古, *娲, *羲], chineseNameArray=[*古, *娲, *羲]}
    脱敏后原始: UserEntryBaseType{chineseNameList=[盘古, 女娲, 伏羲], chineseNameArray=[盘古, 女娲, 伏羲]}
    
    • 1.
    • 2.
    • 3.
     
     

    放在对象属性上

    • 演示对象

    这里的 User 和上面的 User 对象一致。

    public class UserEntryObject {
    
        @SensitiveEntry
        private User user;
    
        @SensitiveEntry
        private List<User> userList;
    
        @SensitiveEntry
        private User[] userArray;
        
        //...
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
     
     
    • 对象构建
    /**
     * 构建用户-属性为列表,数组。列表中为对象。
     * @return 构建嵌套信息
     * @since 0.0.2
     */
    public static UserEntryObject buildUserEntryObject() {
        UserEntryObject userEntryObject = new UserEntryObject();
        User user = buildUser();
        User user2 = buildUser();
        User user3 = buildUser();
        userEntryObject.setUser(user);
        userEntryObject.setUserList(Arrays.asList(user2));
        userEntryObject.setUserArray(new User[]{user3});
        return userEntryObject;
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
     
     
    • 测试演示
    /**
     * 用户属性中有集合或者对象,集合中属性是对象-脱敏测试
     * @since 0.0.2
     */
    @Test
    public void sensitiveEntryObjectTest() {
        UserEntryObject userEntryObject = DataPrepareTest.buildUserEntryObject();
        System.out.println("脱敏前原始: " + userEntryObject);
        UserEntryObject sensitiveUserEntryObject = SensitiveUtil.desCopy(userEntryObject);
        System.out.println("脱敏对象: " + sensitiveUserEntryObject);
        System.out.println("脱敏后原始: " + userEntryObject);
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
     
     
    • 测试结果
    脱敏前原始: UserEntryObject{user=User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}, userList=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}], userArray=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}]}
    脱敏对象: UserEntryObject{user=User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}, userList=[User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}], userArray=[User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}]}
    脱敏后原始: UserEntryObject{user=User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}, userList=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}], userArray=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}]}
    
    • 1.
    • 2.
    • 3.
     
     

    需求 & BUGS

    issues

    欢迎加入开发

    如果你对本项目有兴趣,并且对代码有一定追求,可以申请加入本项目开发。

    如果你善于写文档,或者愿意补全测试案例,也非常欢迎加入。

  • 相关阅读:
    [代码保留]ORA01033: ORACLE initialization or shutdown in progress
    我的重构步骤:重构两份过程一致、中间数据类型不一致的超长函数
    C# 由UTF8 BOM头引发的两个问题(C#去BOM头)
    XPath 基本语法
    Check Browser Compatibility for CSS3 and HTML5 features
    【Mrak】C# 文本文件 ANSI编码格式 转 UTF8
    SQL 过滤重复数据
    重构具有相似“过程”的代码的经验总结(以方法为例)
    POS 收款机资料整理
    WinForm中的暗杀杀手——相对路径
  • 原文地址:https://www.cnblogs.com/shoshana-kong/p/14976898.html
Copyright © 2020-2023  润新知