记录 Spring Security OAuth2 身份验证失败的推荐方法是啥?

Posted

技术标签:

【中文标题】记录 Spring Security OAuth2 身份验证失败的推荐方法是啥?【英文标题】:What is the recommended approach for logging Spring Security OAuth2 authentication failures?记录 Spring Security OAuth2 身份验证失败的推荐方法是什么? 【发布时间】:2020-12-03 16:12:00 【问题描述】:

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

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

我们的客户偶尔会报告我们在应用程序日志中找不到的 401 响应,因此我们假设它来自 Spring Security 本身。我们想记录身份验证和授权失败,但我不知道当前首选的方法是什么。

快速搜索发现 4 条路径:

自定义AuthenticationFailureHandler(好像这个只在FormLoginConfigurer中可用,不是我们的情况) custom AuthenticationEntryPoint(这个可以在 JWT 中使用,但是我要扩展的 OAuth2AuthenticationEntryPoint 是deprecated with an unhelpful message)。我可以尝试改用 HttpStatusEntryPoint,但我想避免更改应用的当前行为,只想添加日志记录。 自定义过滤器(可能不是最干净的路径) AuditApplicationEvents — 看起来很漂亮,但似乎只适用于 Spring Boot。

谁能给我一些指导?

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于记录 Spring Security OAuth2 身份验证失败的推荐方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Spring Security 入门(1-3)Spring Security oauth2.0 指南

Spring security 4.x 往 5.x 升级的坑 (OAuth2)

Spring Security Oauth2

Spring-Security OAuth2 设置 - 无法找到 oauth2 命名空间处理程序

Spring Security OAuth2 v5:NoSuchBeanDefinitionException:'org.springframework.security.oauth2.jwt.Jwt

针对授权标头的Spring Security OAuth2 CORS问题