Java日志脱敏框架 sensitive

项目介绍

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

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

特性

  • 基于注解的日志脱敏。
  • 可以自定义策略实现,策略生效条件。
  • 常见的脱敏内置方案。
  • java 深拷贝,且原始对象不用实现任何接口。
  • 支持用户自定义注解。

自定义注解

maven 导入

1<dependency> 2 <groupId>com.github.houbb</groupId> 3 <artifactId>sensitive</artifactId> 4 <version>0.0.4</version> 5</dependency>

自定义注解

v0.0.4 新增功能。允许功能自定义条件注解和策略注解。 案例

自定义注解

  • 策略脱敏

    /**

    • 自定义密码脱敏策略
    • @author binbin.hou
    • date 2019/1/17
    • @since 0.0.4 */ @Inherited @Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @SensitiveStrategy(CustomPasswordStrategy.class) public @interface SensitiveCustomPasswordStrategy { }
  • 脱敏生效条件

    /**

    • 自定义密码脱敏策略生效条件
    • @author binbin.hou
    • date 2019/1/17
    • @since 0.0.4 */ @Inherited @Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @SensitiveCondition(ConditionFooPassword.class) public @interface SensitiveCustomPasswordCondition{ }
  • TIPS @SensitiveStrategy

策略单独使用的时候,默认是生效的。

如果有 @SensitiveCondition 注解,则只有当条件满足时,才会执行脱敏策略。

@SensitiveCondition 只会对系统内置注解和自定义注解生效,因为 @Sensitive 有属于自己的策略生效条件。

  • 策略优先级 @Sensitive

优先生效,然后是系统内置注解,最后是用户自定义注解。

对应的实现

两个元注解 @SensitiveStrategy@SensitiveCondition 分别指定了对应的实现。

  • CustomPasswordStrategy.java

    public class CustomPasswordStrategy implements IStrategy {

    1@Override 2public Object des(Object original, IContext context) { 3 return "**********************"; 4}

    }

  • ConditionFooPassword.java

    /**

    • 让这些 123456 的密码不进行脱敏
    • @author binbin.hou
    • date 2019/1/2
    • @since 0.0.1 */ public class ConditionFooPassword implements ICondition { @Override public boolean valid(IContext context) { try { Field field = context.getCurrentField(); final Object currentObj = context.getCurrentObject(); final String name = (String) field.get(currentObj); return !name.equals("123456"); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }

    }

定义测试对象

定义一个使用自定义注解的对象。

1public class CustomPasswordModel { 2 3 @SensitiveCustomPasswordCondition 4 @SensitiveCustomPasswordStrategy 5 private String password; 6 7 @SensitiveCustomPasswordCondition 8 @SensitiveStrategyPassword 9 private String fooPassword; 10 11 //其他方法 12}

测试

1/** 2 * 自定义注解测试 3 */ 4@Test 5public void customAnnotationTest() { 6 final String originalStr = "CustomPasswordModel{password='hello', fooPassword='123456'}"; 7 final String sensitiveStr = "CustomPasswordModel{password='**********************', fooPassword='123456'}"; 8 CustomPasswordModel model = buildCustomPasswordModel(); 9 Assert.assertEquals(originalStr, model.toString()); 10 11 CustomPasswordModel sensitive = SensitiveUtil.desCopy(model); 12 Assert.assertEquals(sensitiveStr, sensitive.toString()); 13 Assert.assertEquals(originalStr, model.toString()); 14}

构建对象的方法如下:

1/** 2 * 构建自定义密码对象 3 * @return 对象 4 */ 5private CustomPasswordModel buildCustomPasswordModel(){ 6 CustomPasswordModel model = new CustomPasswordModel(); 7 model.setPassword("hello"); 8 model.setFooPassword("123456"); 9 return model; 10}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

java 日志的数据脱敏

思路1。在model层进行处理,直接重写get方法,在写一个getPlain获取明文方法。(缺点:数据库写入和json序列化传递时使用的都是密文)  2.利用日志组件过滤特定的key,去进行脱敏(缺点:对所有的日志输出全部要正则匹配,非常耗时。)由1,2的利弊,肯定会选择1,然后考虑一种实现(在model层定义方法,获取它的一个复制类,复制

利用Jackson序列化实现数据脱敏

在项目中有些敏感信息不能直接展示,比如客户手机号、身份证、车牌号等信息,展示时均需要进行数据脱敏,防止泄露客户隐私。脱敏即是对数据的部分信息用脱敏符号()处理。