在我们的日志中,由于人们在应用程序中使用了CC信息,我们看到了信用卡号码(我不知道他们为什么这样做)。我们想对这些信息进行消毒(由于PCI的考虑),甚至不将其持续到磁盘上。

因此,我希望能够在登录日志文件之前对日志条目进行消毒。我一直在看tomcat阀(访问日志阀)。这是一条路吗?

有帮助吗?

解决方案

我能够通过扩展来解决这个问题 AccessLogValve 和覆盖 public log(java.lang.String message):

public class SanitizedAccessLogValve extends AccessLogValve {

    private static Pattern pattern = Pattern.compile("\\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})\\b");

    /*
     This method will sanitize any cc numbers in the string and replace them with x's
    */
    private String sanitize(String string) {
        String sanitizedString = string;

        if(string != null) {

            StringBuffer buffer = new StringBuffer();
            Matcher matcher = pattern.matcher(string);

            while(matcher.find()) {
                MatchResult matchResult = matcher.toMatchResult();

                int start = matchResult.start();
                int end = matchResult.end();

                String matchedText = string.substring(start, end);

                matcher.appendReplacement(buffer, "xxxxxxxxxxxxxxxx");                
            }

            matcher.appendTail(buffer);

            sanitizedString = buffer.toString();
        }

        return sanitizedString;
    }

    @Override
    public void log(String message) {
        super.log(sanitize(message));
    }
}

您需要将其编译到一个罐子中,然后将该罐子文件放入 $CATALINA_HOME/lib.

然后在你 server.xml:

<Valve className="my.valves.SanitizedAccessLogValve"
       directory="access_logs"  prefix="localhost." suffix=".log"
       pattern='%v %h %t "%r" %s %B %T "%{User-Agent}i"'/>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top