审计
一旦启用了 Spring Security,Spring Boot Actuator 提供了一个灵活的审计框架,用于发布事件(默认情况下包括“认证成功”、“认证失败”和“访问被拒绝”异常)。这一特性在生成报告以及基于认证失败实现锁定策略时非常有用。
你可以在应用程序的配置中提供一个类型为 AuditEventRepository 的 Bean 来启用审计功能。为了方便,Spring Boot 提供了 InMemoryAuditEventRepository。InMemoryAuditEventRepository 的功能有限,我们建议仅在开发环境中使用它。对于生产环境,请考虑创建你自己的替代 AuditEventRepository 实现。
自定义审计
要自定义发布的安全事件,你可以提供自己的 AbstractAuthenticationAuditListener 和 AbstractAuthorizationAuditListener 实现。
你也可以为自己的业务事件使用审计服务。为此,可以将 AuditEventRepository bean 注入到自己的组件中并直接使用,或者通过 Spring 的 ApplicationEventPublisher 发布一个 AuditApplicationEvent(通过实现 ApplicationEventPublisherAware)。