• 高性能日志脱敏组件:已支持 log4j2 和 logback 插件


    项目介绍

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

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

    同时支持 logback 和 log4j2 等常见的日志脱敏插件。

    拓展阅读

    金融用户敏感数据如何优雅地实现脱敏?

    日志脱敏之后,无法根据信息快速定位怎么办?

    log4j2 插件统一脱敏

    说明

    上面的方法非常适用于新的项目,按照响应的规范进行推广。

    但是很多金融公司都有很多历史遗留项目,或者使用不规范,比如使用 map 等,导致上面的方法在脱敏技改时需要耗费大量的时间,而且回溯成本很高。

    有没有什么方法,可以直接在日志层统一处理呢?

    log4j2 Rewrite

    我们可以基于 log4j2 RewritePolicy 统一使用脱敏策略。

    说明:如果使用 slf4j 接口,实现为 log4j2 时也是支持的。

    使用入门

    maven 引入

    引入核心脱敏包。

    <dependency>
        <groupId>com.github.houbbgroupId>
        <artifactId>sensitive-log4j2artifactId>
        <version>1.6.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    其他的一般项目中也有,如 log4j2 包:

    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-apiartifactId>
        <version>${log4j2.version}version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>${log4j2.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    log4j2.xml 配置

    例子如下:

    
    <Configuration status="WARN" packages = "com.github.houbb.sensitive.log4j2.layout">
    
        <Properties>
            <Property name="DEFAULT_PATTERN">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nProperty>
            <Property name="DEFAULT_CHARSET">UTF-8Property>
        Properties>
    
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <SensitivePatternLayout/>
            Console>
        Appenders>
    
        <Loggers>
            <Root level="DEBUG">
                <AppenderRef ref="Console"/>
            Root>
        Loggers>
    
    Configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    几个步骤:

    1. 指定 package 为 packages = "com.github.houbb.sensitive.log4j2.layout"

    2. 按照 log4j2 layout 规范,指定 Layout 策略为 SensitivePatternLayout

    测试

    正常的日志打印:

    private static final String TEST_LOG = "mobile:13088887777; bankCard:6217004470007335024, email:mahuateng@qq.com, amount:123.00, " +
            "IdNo:340110199801016666, name1:李明, name2:李晓明, name3:李泽明天, name4:山东小栗旬" +
            ", birthday:20220517, GPS:120.882222, IPV4:127.0.0.1, address:中国上海市徐汇区888号;";
    
    logger.info(TEST_LOG);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自动脱敏效果如下:

    01:37:28.010 [main] INFO  com.github.houbb.sensitive.test.log4j2.Log4j2AndSlf4jLayoutTest - mobile:130****7777|9FC4D36D63D2B6DC5AE1297544FBC5A2; bankCard:6217***********5024|444F49289B30944AB8C6C856AEA21180, email:mahu*****@qq.com|897915594C94D981BA86C9E83ADD449C, amount:123.00, IdNo:340110199801016666, name1:李明, name2:李晓明, name3:李泽明天, name4:山东小栗旬, birthday:20220517, GPS:120.882222, IPV4:127.0.0.1, address:中国上海市徐******|821A601949B1BD18DCBAAE27F2E27147;
    
    • 1

    ps: 这里是为了演示各种效果,实际默认对应为 1,2,3,4,9 这几种策略。

    log4j2 配置定制化

    为了满足各种用户的场景,在 V1.6.0 引入了 SensitivePatternLayout 策略的可配置化。

    用户可以在应用 resources 下通过 chars-scan-config.properties 配置文件指定。

    默认配置

    log4j2 配置中,SensitivePatternLayout 配置默认为:

    chars.scan.prefix=::,,'"‘“=| +()()
    chars.scan.scanList=1,2,3,4,9
    chars.scan.replaceList=1,2,3,4,9
    chars.scan.defaultReplace=12
    chars.scan.replaceHash=md5
    chars.scan.whiteList=""
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    属性说明

    SensitivePatternLayout 策略的属性说明。

    属性说明默认值备注
    prefix需要脱敏信息的匹配前缀::,,'"‘“= +()() 和英文竖线降低误判率
    replaceHash哈希策略模式md5支持 md5/none 两种模式
    scanList敏感扫描策略列表1,2,3,41~10 内置的10种敏感信息扫描策略,多个用逗号隔开
    replaceList敏感替换策略列表1,2,3,41~10 内置的10种敏感信息替换策略,多个用逗号隔开
    defaultReplace敏感替换默认策略121~13 内置的13种敏感信息替换策略,指定一个。当列表没有匹配时,默认使用这个
    whiteList白名单``希望跳过处理的白名单信息

    其中 1-13 的内置策略说明如下:

    策略标识说明
    1手机号
    2身份证
    3银行卡
    4邮箱
    5中国人名
    6出生日期
    7GPS
    8IPV4
    9地址
    10护照
    11匹配任意不掩盖
    12匹配任意半掩盖
    13匹配任意全掩盖

    不足之处

    这里的策略自定义和 log4j2 的插件化比起来,确实算不上强大,但是可以满足 99% 的脱敏场景。

    后续有时间考虑类似 log4j2 的 plugins 思想,实现更加灵活的自定义策略。

    logback 脱敏插件

    说明

    为了便于用户使用,v1.6.0 开始支持 logback 插件模式。

    使用入门

    maven 引入

    引入 logback 依赖包

    <dependency>
        <groupId>ch.qos.logbackgroupId>
        <artifactId>logback-classicartifactId>
        <version>${logback.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    指定 logback.xml 配置

    <configuration>
        
        <conversionRule conversionWord="sensitive" converterClass="com.github.houbb.sensitive.logback.converter.SensitiveLogbackConverter" />
        
        <appender name="STDOUTConverter" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %sensitive%npattern>
            encoder>
        appender>
    
        
        <appender name="STDOUTLayout" class="ch.qos.logback.core.ConsoleAppender">
            <layout class="com.github.houbb.sensitive.logback.layout.SensitiveLogbackLayout">
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
            layout>
        appender>
    
        
        <root level="DEBUG">
            <appender-ref ref="STDOUTConverter"/>
            <appender-ref ref="STDOUTLayout"/>
        root>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    这里共计支持 Converter 和 Layout 两种模式,任选一个即可。

    建议使用 SensitiveLogbackConverter,脱敏日志内容。

    日志效果

    脱密效果和 log4j2 类似,如下:

    01:42:32.579 [main] INFO  c.g.h.sensitive.test2.LogbackMain - mobile:130****7777|9FC4D36D63D2B6DC5AE1297544FBC5A2; bankCard:6217***********5024|444F49289B30944AB8C6C856AEA21180, email:mahu*****@qq.com|897915594C94D981BA86C9E83ADD449C, amount:123.00, " + "IdNo:340110199801016666, name1:李明, name2:李晓明, name3:李泽明天, name4:山东小栗旬" + ", birthday:20220517, GPS:120.882222, IPV4:127.0.0.1, address:中国上海市徐******|821A601949B1BD18DCBAAE27F2E27147;
    
    • 1

    配置属性

    同 log4j2,此处不再赘述。

    小结

    基于日志插件的方式更加方便,便于项目的推广使用,实用性更强。

    项目的开源地址

    https://github.com/houbb/sensitive

  • 相关阅读:
    【C语言】调试
    第8章 MySQL的数据目录
    vue中,封装组件demo
    Java“牵手”唯品会商品列表页数据采集+唯品会商品价格数据排序,唯品会API接口申请指南
    Redhat(3)-Bash-Shell-正则表达式
    Python变量与注释高级用法
    go开发脚手架 动态/静态路由 (根据gin框架)
    oracle 函数 触发器
    Vue系列之Vue实例化
    [AGC057D] Sum Avoidance
  • 原文地址:https://blog.csdn.net/ryo1060732496/article/details/133917464