java禁用Spring安全日志
我们正在使用自定义筛选器进行身份验证
我们的自定义过滤器从BasicAuthenticationFilter扩展而来。我们在应用程序中只使用基本身份验证。我们是这样做的,因为我们希望在我们的系统中处理不同的身份验证场景 身份验证入口点
每当调用API时,我们的过滤器就会启动,这会导致控制台上打印大量日志。 这些日志本身来自BasicAuthenticationFilter。类(我们的筛选器已扩展)
if (this.authenticationIsRequired(username)) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, tokens[1]);
authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
Authentication authResult = this.authenticationManager.authenticate(authRequest);
if (debug) {
this.logger.debug("Authentication success: " + authResult);
}
SecurityContextHolder.getContext().setAuthentication(authResult);
this.rememberMeServices.loginSuccess(request, response, authResult);
this.onSuccessfulAuthentication(request, response, authResult);
}
是否可以避免此日志记录,而不必实际重写该函数
# 1 楼答案
在springboot中,日志记录级别可以设置为
TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
。在应用程序中设置以下内容。yml或应用程序。属性可以配置根记录器级别:除此之外,springboot还允许根据组或类设置日志记录级别
实际上,springbobot为我们提供了为类设置日志记录级别的功能。因此,在您的例子中,您可以将过滤器类的日志记录级别设置为
ERROR
,这样就不会从中生成不需要的日志,或者将其设置为OFF
。另一种方法是关闭整个spring安全组的日志记录(不建议这样做,因为可能会丢失很多有用的日志):阅读Doc
# 2 楼答案
处理同一问题有多种方法
从命令行
来自记录器配置
您可以为不同的类设置不同的日志级别,关闭所需类的日志记录
根据Spring page here26.4日志级别
详细阅读教程here