https://docs.spring.io/spring-boot/docs/3.2.0/reference/htmlsingle/#actuator.auditing
一旦 Spring Security 开始工作,Spring Boot Actuator 就有一个灵活的审计框架,该框架会发布事件(默认情况下,是“认证成功”、“失败”和“拒绝访问”异常)。这个特性对于报告和基于认证失败实现锁定策略非常有用。
你可以通过在应用程序的配置中提供一个类型为 AuditEventRepository
的 bean 来启用审计。为了方便起见,Spring Boot 提供了一个 InMemoryAuditEventRepository
。但是,InMemoryAuditEventRepository
的功能有限,推荐仅在开发环境中使用它。对于生产环境,请考虑创建你自己的 AuditEventRepository
实现。
自定义审计
为了自定义发布的安全事件,你可以提供 AbstractAuthenticationAuditListener
和 AbstractAuthorizationAuditListener
的自定义实现。
你还可以使用审计服务来处理自己的业务事件。要做到这一点,你可以将 AuditEventRepository
bean 注入到你自己的组件中并直接使用它,或者通过实现 ApplicationEventPublisherAware
来发布一个 AuditApplicationEvent
,并使用 Spring 的 ApplicationEventPublisher
。