logback日志脱敏

文章目录

      • 一、概述
      • 方式一(不推荐):
      • 方式二:

一、概述

logback日志脱敏。

Github| https://github.com/duke147/springboot-logback

方式一(不推荐):

这种方式只能脱敏一个字段。

因为 replace 的规则在找到第一个匹配后就会立即停止,这就导致只有第一个匹配到的字段被替换。

        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %replace(%msg){
                'bankNo":"(\d{4})\d+(\d{4})', 'bankNo":"$1****$2'
                }%n
            </pattern>
        </encoder>

方式二:

自定义匹配规则,新建类MaskingConverter.java

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MaskingConverter extends ClassicConverter {
    /*********** 匹配JSON格式字段:"name":"张三" ************/
    // 银行卡脱敏
    private static final Pattern BANKNO_PATTERN = Pattern.compile("\"bankNo\":\"(\\d{4})\\d+(\\d{4})\"");
    // 姓名脱敏
    private static final Pattern NAME_PATTERN = Pattern.compile("\"(legalName|name|personName)\":\"([\\u4e00-\\u9fa5])([\\u4e00-\\u9fa5]*)\"");
    // 手机号脱敏
    private static final Pattern PHONE_PATTERN = Pattern.compile("\"(legalPhone|mobilePhone|adminPhone)\":\"(\\d{3})\\d+(\\d{4})\"");
    // 身份证脱敏
    private static final Pattern IDNUM_PATTERN = Pattern.compile("\"(idNum|adminIdentNo)\":\"(\\d{3})\\d{8}(\\d*)\"");

    /*********** 匹配等号格式字段:name=张三 ************/
    // 手机号脱敏
    private static final Pattern EQ_PHONE_PATTERN = Pattern.compile("(adminPhone|mobilePhone)=(\\d{3})\\d+(\\d{4})");
    // 姓名脱敏
    private static final Pattern EQ_NAME_PATTERN = Pattern.compile("(adminName|personName)=([\\u4e00-\\u9fa5])([\\u4e00-\\u9fa5]*)");
    // 身份证脱敏
    private static final Pattern EQ_IDNUM_PATTERN = Pattern.compile("(adminIdentNo)=(\\d{3})\\d{8}(\\d*)");

    @Override
    public String convert(ILoggingEvent event) {
        String message = event.getFormattedMessage();

        Matcher matcher = BANKNO_PATTERN.matcher(message);
        if (matcher.find()) {
            String maskedBankNo = "\"bankNo\":\"" + matcher.group(1) + "****" + matcher.group(2) + "\"";
            message = matcher.replaceFirst(maskedBankNo);
        }

        matcher = NAME_PATTERN.matcher(message);
        while (matcher.find()) {
            String maskedName = "\"" + matcher.group(1) + "\":\"*" + matcher.group(3) + "\"";
            message = matcher.replaceFirst(maskedName);
            matcher = NAME_PATTERN.matcher(message);
        }

        matcher = PHONE_PATTERN.matcher(message);
        while (matcher.find()) {
            String maskedPhone = "\"" + matcher.group(1) + "\":\"" + matcher.group(2) + "****" + matcher.group(3) + "\"";
            message = matcher.replaceFirst(maskedPhone);
            matcher = PHONE_PATTERN.matcher(message);
        }

        matcher = IDNUM_PATTERN.matcher(message);
        if (matcher.find()) {
            String maskedIdNum = "\"" + matcher.group(1) + "\":\"" + matcher.group(2) + "********" + matcher.group(3) + "\"";
            message = matcher.replaceFirst(maskedIdNum);
        }

        matcher = EQ_PHONE_PATTERN.matcher(message);
        while (matcher.find()) {
            String maskedPhone = matcher.group(1) + "=" + matcher.group(2) + "****" + matcher.group(3);
            message = matcher.replaceFirst(maskedPhone);
            matcher = EQ_PHONE_PATTERN.matcher(message);
        }

        matcher = EQ_NAME_PATTERN.matcher(message);
        while (matcher.find()) {
            String maskedName = matcher.group(1) + "=*" + matcher.group(3);
            message = matcher.replaceFirst(maskedName);
            matcher = EQ_NAME_PATTERN.matcher(message);
        }

        matcher = EQ_IDNUM_PATTERN.matcher(message);
        if (matcher.find()) {
            String maskedIdNum = matcher.group(1) + "=" + matcher.group(2) + "********" + matcher.group(3);
            message = matcher.replaceFirst(maskedIdNum);
        }

        return message;
    }
}

配置:

<configuration>
    <!--************************** 开发环境 ****************************-->
    <!-- 日志根路径(绝对路径) -->
    <!--    <property name="log.basePath" value="/"/>-->
    <!-- 日志根路径(相对路径),如果使用外部 tomcat,则日志会在bin目录,需要加../ -->
    <property name="log.basePath" value="logs-dev"/>
    <!-- 日志存放路径 -->
    <property name="log.path" value="/" />
    <!-- 自定义日志转换规则 -->
    <conversionRule conversionWord="mask" converterClass="com.example.springbootlogback.MaskingConverter" />
    <!-- 不同的appender,统一日志输出格式 -->
    <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %mask%n" />

    <!-- 控制台输出 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>
</configuration>

你可能感兴趣的:(J2EE,logback,java,日志脱敏)