【问题标题】:Sanitizing Tomcat access log entries清理 Tomcat 访问日志条目
【发布时间】:2011-04-28 00:23:11
【问题描述】:
在我们的日志中,我们看到信用卡号码是因为人们使用 CC 信息在我们的应用中点击了某些 ULR(我不知道他们为什么要这样做)。我们希望清理这些信息(出于 PCI 考虑),甚至不将其保存到磁盘。
因此,我希望能够在日志条目到达日志文件之前对其进行清理。我一直在研究 Tomcat Valves(访问日志阀)。这是要走的路吗?
【问题讨论】:
标签:
security
tomcat
logging
log4j
pci-compliance
【解决方案1】:
我能够通过扩展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));
}
}
你需要把它编译成一个jar,然后把那个jar文件放到$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"'/>