建议使用哪种方法记录Spring Security OAuth2身份验证失败?

问题描述

我正在使用Spring Web应用程序(不是Spring Boot),该应用程序使用Spring Security 5.3.3,Spring Security OAuth 2.5.0并进行如下配置:

http.authorizeRequests().
    antMatchers(permitUrls).
    permitAll().
    anyRequest().
    authenticated().
    and().
    oauth2ResourceServer().
    jwt();

我们的客户偶尔会报告我们在应用程序日志中找不到的401响应,因此我们假设它来自Spring Security本身。我们想记录身份验证和授权失败的情况,但是我对目前实现该目标的首选方法感到困惑。

快速搜索会发现4条路径:

@H_502_10@
  • 自定义AuthenticationFailureHandler(似乎仅在formLoginConfigurer中可用,而不是这种情况)
  • 自定义AuthenticationEntryPoint(这是JWT可用的,但是我要扩展的OAuth2AuthenticationEntryPoint是deprecated with an unhelpful message)。我可以尝试改用HttpStatusEntryPoint,但我想避免更改应用程序的当前行为,只想添加日志记录。
  • 自定义过滤器(可能不是最干净的路径)
  • AuditApplicationEvents-看起来很漂亮,但似乎仅适用于Spring Boot。
  • 有人可以为此提供一些指导吗?

    解决方法

    我建议通过将org.springframework.security的记录器级别更改为DEBUG来激活它。 根据您的日志工具(log4j2,logback等),在日志配置中创建名称为org.springframework.security和级别为DEBUG的记录器。