【发布时间】:2021-08-17 14:05:47
【问题描述】:
我正在开发一个通过 URL 查询字符串以纯文本形式发送一些敏感信息的应用程序。我的目标是自定义我的码头请求日志,以便在记录之前编辑或删除任何敏感信息。
到目前为止,我已经尝试制作一个自定义过滤器,它确实从查询字符串中删除了敏感信息,但是当我保持启用本机码头日志记录时,我得到了双重日志(一个有密码,一个没有密码),当我禁用本机时我没有收到无效流量(请求到达正确的服务但查询字符串有其他问题)。
我的问题是,有没有办法继续使用我的过滤器而不让码头记录具有相同信息的重复条目,或者是否有不同的方法来清理密码(如何实现不同的记录器,修改码头requestlog jar?)。我对开发世界几乎是全新的,所以我不知道有什么可能。
过滤以供参考
@Component
@Order(1)
public class RequestLogFilter implements Filter {
private final static Logger log = LogManager.getLogger(RequestLogFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
String uriPath = req.getRequestURI();
String uri = req.getQueryString();
uri = uri != null ? uri.replaceAll("password.+?&", "password=redacted&") : "";
String method = req.getMethod();
String ip = req.getRemoteAddr();
String protocol = req.getProtocol();
String logString = ip + " - - " + '"' + method + " " + uriPath + "?" + uri + " " + protocol + '"';
log.info(logString);
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
log.warn("Destructing RequestLogFilter :{}");
}
}
【问题讨论】: