我正在使用
CommonsRequestLoggingFilter
来记录传入请求的有效负载。但对于一种方法,我不想记录有效负载,因为它包含用户机密数据。我在web.xml
中有以下配置
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>org.springframework.web.filter.CommonsRequestLoggingFilter</filter-class>
<init-param>
<param-name>includePayload</param-name>
<param-value >true</param-value>
</init-param>
<init-param>
<param-name>includeQueryString</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>MaxPayloadLength</param-name>
<param-value>10000</param-value>
</init-param>
</filter>
帮我用一种方法来限制。
提前致谢
您可以创建自己的类来扩展 CommonsRequestLoggingFilter,在该类中您可以执行以下操作:-
@Override
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
//based on uri you could print payload or not.
}
对我有用的是:
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter() {
@Override
protected boolean shouldLog(HttpServletRequest request) {
if (!request.getRequestURI().startsWith("/myapp/service1/")) {
return false;
}
return logger.isDebugEnabled();
}
};
return filter;
}
虽然我对这个解决方案不太满意,但仍在尝试寻找更优雅的东西。