有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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);
            }

是否可以避免此日志记录,而不必实际重写该函数


共 (2) 个答案

  1. # 1 楼答案

    在springboot中,日志记录级别可以设置为TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF。在应用程序中设置以下内容。yml或应用程序。属性可以配置根记录器级别:

    logging.level.root=warn
    

    除此之外,springboot还允许根据组或类设置日志记录级别

    It’s often useful to be able to group related loggers together so that they can all be configured at the same time. For example, you might commonly change the logging levels for all Tomcat related loggers, but you can’t easily remember top level packages.

    To help with this, Spring Boot allows you to define logging groups in your Spring Environment. For example, here’s how you could define a “tomcat” group by adding it to your application.properties:

    logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat

    实际上,springbobot为我们提供了为类设置日志记录级别的功能。因此,在您的例子中,您可以将过滤器类的日志记录级别设置为ERROR,这样就不会从中生成不需要的日志,或者将其设置为OFF。另一种方法是关闭整个spring安全组的日志记录(不建议这样做,因为可能会丢失很多有用的日志):

    logging.level.com.security.BasicAuthenticationFilter=ERROR
    

    阅读Doc

  2. # 2 楼答案

    处理同一问题有多种方法

    从命令行

    -Dlogging.level.org.springframework.security.web.authentication.www=OFF
    

    来自记录器配置

    您可以为不同的类设置不同的日志级别,关闭所需类的日志记录

    根据Spring page here26.4日志级别

    All the supported logging systems can have the logger levels set in the Spring Environment (for example, in application.properties) by using logging.level.= where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root.

    详细阅读教程here