【问题标题】:How do I enable logging for Spring Security?如何为 Spring Security 启用日志记录?
【发布时间】:2015-08-31 13:03:13
【问题描述】:

我正在设置 Spring Security 来处理登录用户。我已经以用户身份登录,并在成功登录后被带到拒绝访问错误页面。我不知道我的用户实际上被分配了哪些角色,或者导致访问被拒绝的规则,因为我不知道如何为 Spring Security 库启用调试。

我的安全 xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... >
    <!-- security -->

    <security:debug/><!-- doesn't seem to be working -->

    <security:http auto-config="true">

        <security:intercept-url pattern="/Admin**" access="hasRole('PROGRAMMER') or hasRole('ADMIN')"/>
        <security:form-login login-page="/Load.do"
            default-target-url="/Admin.do?m=loadAdminMain"
            authentication-failure-url="/Load.do?error=true"
            username-parameter="j_username"
            password-parameter="j_password"
            login-processing-url="/j_spring_security_check"/>
        <security:csrf/><!-- enable Cross Site Request Forgery protection -->
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="loginDataSource"
                users-by-username-query="SELECT username, password, active FROM userinformation WHERE username = ?"
                authorities-by-username-query="
                    SELECT ui.username, r.rolename 
                    FROM role r, userrole ur, userinformation ui 
                    WHERE ui.username=? 
                    AND ui.userinformationid = ur.userinformationid 
                    AND ur.roleid = r.roleid "
            />
            <security:password-encoder hash="md5"/>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

我也尝试将log4j.logger.org.springframework.security=DEBUG 添加到我的 log4j.properties

如何获取 Spring Security 的调试输出?

【问题讨论】:

  • 检查这个link如果这可以帮助你。
  • @pise 您可以将其添加为答案(至少包含相关的摘录/摘要),以便我可以将其标记为已解决?
  • 查看这个问题的答案:stackoverflow.com/questions/7840088/…
  • 嘿 - 尝试将其添加为答案,然后将其转换为评论。

标签: debugging spring-security


【解决方案1】:

假设您使用的是 Spring Boot,另一种选择是将以下内容放入您的 application.properties:

logging.level.org.springframework.security=DEBUG

大多数其他 Spring 模块也是如此。

如果您不使用 Spring Boot,请尝试在日志记录配置中设置属性,例如回退。

这也是 application.yml 版本:

logging:
  level:
    org:
      springframework:
        security: DEBUG

【讨论】:

  • 这是否假定 Spring Boot?
  • @JohnCamerin 是的,确实如此。在 application.properties 中设置日志级别是 Spring Boot 的一项功能。如果您不使用 Spring Boot,您可以通过其他方式(例如在您的 logback.xml 中)设置日志级别 org.springframework.security
  • 添加 org.springframework.web.cors 以启用 Cors 处理器日志。
【解决方案2】:

您可以使用@EnableWebSecurity 注释的选项轻松启用调试支持:

@EnableWebSecurity(debug = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    …
}

【讨论】:

  • EnableWebFluxSecurity 怎么样,它没有调试选项
  • 啊,有趣。但是,我没有使用 WebFlux 的经验。
  • 有没有办法从 application.properties 控制这个标志
【解决方案3】:

使用Spring的DebugFilter进行基本调试可以这样配置:

@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.debug(true);
    }
}

【讨论】:

  • 那是一些相当弱的调试日志。它只打印出请求标头和“安全过滤器链”。在跟踪访问问题时根本没有用。
【解决方案4】:

您可以使用@EnableWebSecurity 注解的选项轻松启用调试支持:

@EnableWebSecurity(debug = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    …
}

如果您需要在您的 application-{profile}.properties 文件中进行配置文件特定的控制

org.springframework.security.config.annotation.web.builders.WebSecurity.debugEnabled=false

获取详细帖子:http://www.bytefold.com/enable-disable-profile-specific-spring-security-debug-flag/

【讨论】:

    【解决方案5】:

    我们总是可以通过以下配置检查 Spring Security 中注册的过滤器

    1. @EnableWebSecurity(debug=true) - 我们需要启用安全细节的调试
    2. 通过在 application.properties logging.level.org.springframework.security.web.FilterChainProxy=DEBUG 中添加以下属性来启用详细信息的日志记录

    下面提到一些在身份验证流程中执行的 Spring Security 内部过滤器:

    Security filter chain: [
      CharacterEncodingFilter
      WebAsyncManagerIntegrationFilter
      SecurityContextPersistenceFilter
      HeaderWriterFilter
      CsrfFilter
      LogoutFilter
      X509AuthenticationFilter
      UsernamePasswordAuthenticationFilter
      RequestCacheAwareFilter
      SecurityContextHolderAwareRequestFilter
      RememberMeAuthenticationFilter
      AnonymousAuthenticationFilter
      SessionManagementFilter
      ExceptionTranslationFilter
      FilterSecurityInterceptor
    ]
    

    【讨论】:

      【解决方案6】:

      现在可以从 5.4.0-M2 版本开始为 webflux 反应式应用程序提供 Spring 安全日志记录(正如 @bzhu 在评论 How do I enable logging for Spring Security? 中提到的那样)

      直到它进入 GA 版本,这里是如何在 gradle 中获得这个里程碑版本

      repositories {
          mavenCentral()
          if (!version.endsWith('RELEASE')) {
              maven { url "https://repo.spring.io/milestone" }
          }
      }
      
      // Force earlier milestone release to get securing logging preview
      // https://docs.spring.io/spring-security/site/docs/current/reference/html5/#getting-gradle-boot
      // https://github.com/spring-projects/spring-security/pull/8504
      // https://github.com/spring-projects/spring-security/releases/tag/5.4.0-M2
      ext['spring-security.version']='5.4.0-M2'
      dependencyManagement {
          imports {
              mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-25
        • 2012-07-12
        • 2011-09-22
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多