如何为 Spring Security 启用日志记录?

我正在设置 SpringSecurity 来处理登录用户。我已经登录作为一个用户,并采取了访问拒绝错误页面成功登录。我不知道我的用户实际分配了什么角色,也不知道导致访问被拒绝的规则,因为我不知道如何为 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 的调试输出?

154847 次浏览

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

@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {


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

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

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

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

logging.level.org.springframework.security=DEBUG

这对于大多数其他 Spring 模块也是一样的。

如果不使用 Spring Boot,请尝试在日志配置中设置该属性,例如 logback。

下面是 application.yml 版本:

logging:
level:
org:
springframework:
security: DEBUG

您可以使用@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.4.0-M2版本(@ bZhu在注释 如何为 Spring Security 启用日志记录?中提到)开始使用针对 webflow 反应应用程序的 Spring 安全日志记录

在 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}"
}


}

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

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

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

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

使用带有默认 Spring 安全过滤器的 Spring Boot (不需要自定义任何东西,甚至不需要在 EnableWebSecurity注释中设置调试) ,将 TRACE设置为如下 application.properties所示:

logging.level.org.springframework.security=TRACE

足以让它详细说明过滤器被调用和它们正在做什么。

TRACE w.c.HttpSessionSecurityContextRepository : No HttpSession currently exists
TRACE w.c.HttpSessionSecurityContextRepository : Created SecurityContextImpl [Null authentication]
DEBUG w.c.HttpSessionSecurityContextRepository : Created HttpSession as SecurityContext is non-default
...
DEBUG o.s.security.web.FilterChainProxy        : Securing POST /api/product/productname01
TRACE o.s.security.web.FilterChainProxy        : Invoking WebAsyncManagerIntegrationFilter (1/16)
...
TRACE o.s.security.web.FilterChainProxy        : Invoking CsrfFilter (5/16)
DEBUG o.s.security.web.csrf.CsrfFilter         : Invalid CSRF token found for http://localhost/api/product/productname01
DEBUG o.s.s.w.access.AccessDeniedHandlerImpl   : Responding with 403 status code

版本:

Spring Framework Bom version 5.3.16
Spring Boot 2.6.4
Spring 5.3.16
Spring Security 5.6.2