网站的目的,lnmp wordpress 安装,南昌商城建设,订阅号如何申请spring启动执行Spring Boot Actuator提供了审核功能#xff0c;用于在启用了Spring Security的Spring Boot应用程序中发布和侦听与安全相关的事件。 默认事件是身份验证成功#xff0c;身份验证失败和访问被拒绝#xff0c;但是可以使用自定义事件进行扩展。 确保在项目中启… spring启动执行 Spring Boot Actuator提供了审核功能用于在启用了Spring Security的Spring Boot应用程序中发布和侦听与安全相关的事件。 默认事件是身份验证成功身份验证失败和访问被拒绝但是可以使用自定义事件进行扩展。 确保在项目中启用了Spring Boot Security和Actuator dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency执行器 默认情况下 /auditevents端点/auditevents启用状态因此在启动应用程序并使用应用程序日志中提供的用户名user和password登录后您可以看到当前的安全事件 {events: [{timestamp: 2017-03-14T22:59:580000,principal: user,type: AUTHENTICATION_FAILURE,data: {details: {remoteAddress: 0:0:0:0:0:0:0:1,sessionId: null},type: org.springframework.security.authentication.BadCredentialsException,message: Bad credentials}},{timestamp: 2017-03-14T23:00:070000,principal: user,type: AUTHENTICATION_SUCCESS,data: {details: {remoteAddress: 0:0:0:0:0:0:0:1,sessionId: null}}}]
} /auditevents端点接受请求的可选参数 pricipal -主体名称 after –事件发生后的日期格式如下 yyyy-MM-ddTHH:mm:ssZ type –事件类型例如AUTHORIZATION_FAILUREAUTHENTICATION_SUCCESSAUTHENTICATION_FAILUREAUTHENTICATION_SWITCH 请求示例 http// localhost8080 / auditeventstype AUTHORIZATION_FAILUREafter 2017-03-14T233A143A122B0000principal anonymousUser 端点实现使用org.springframework.boot.actuate.audit.AuditEventRepository返回所有已注册的审核事件。 自定义/auditevents端点 您可以使用endpoints.auditevents.*属性来自定义端点。 例如要更改审核事件端点的路径只需使用endpoints.auditevents.path属性。 使用 安全事件由执行器中的org.springframework.boot.actuate.audit.AuditEvent值对象表示。 该对象包含时间戳用户名事件类型和事件数据。 获得有关审核事件的最简单方法是通过Spring的org.springframework.context.event.EventListener订阅org.springframework.boot.actuate.audit.listener.AuditApplicationEvent事件 Component
public class AuditApplicationEventListener {private static final Logger LOG LoggerFactory.getLogger(AuditApplicationEventListener.class);EventListenerpublic void onAuditEvent(AuditApplicationEvent event) {AuditEvent actualAuditEvent event.getAuditEvent();LOG.info(On audit application event: timestamp: {}, principal: {}, type: {}, data: {},actualAuditEvent.getTimestamp(),actualAuditEvent.getPrincipal(),actualAuditEvent.getType(),actualAuditEvent.getData());}
} 输出示例 2017-03-15 00:44:12.921 INFO 13316 --- [nio-8080-exec-1] p.c.d.s.s.AuditApplicationEventListener : On audit event: timestamp: Wed Mar 15 00:44:12 CET 2017, principal: user, type: AUTHENTICATION_SUCCESS, data: {detailsorg.springframework.security.web.authentication.WebAuthenticationDetailsb364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null}异步事件 EventListener是同步的但是如果需要异步行为则可以使用Async注释事件侦听器方法并确保启用了异步例如通过EnableAsync Component
public class AuditApplicationEventListener {private static final Logger LOG LoggerFactory.getLogger(AuditApplicationEventListener.class);EventListenerAsyncpublic void onAuditEvent(AuditApplicationEvent event) {}
} 并配置 SpringBootApplication
EnableAsync
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}
}使用 另外您可以扩展org.springframework.boot.actuate.audit.listener.AbstractAuditListener并覆盖其org.springframework.boot.actuate.audit.listener.AbstractAuditListener#onAuditEvent方法 Component
public class AuditEventListener extends AbstractAuditListener {private static final Logger LOG LoggerFactory.getLogger(AuditEventListener.class);Overrideprotected void onAuditEvent(AuditEvent event) {LOG.info(On audit event: timestamp: {}, principal: {}, type: {}, data: {},event.getTimestamp(),event.getPrincipal(),event.getType(),event.getData());}
} 注意事件存储库中不会存储任何事件因此/auditevents端点将始终返回空数组。 要解决此问题您可以注入审核存储库也可以直接从org.springframework.boot.actuate.audit.listener.AuditListener扩展 Component
public class AuditEventListener extends AbstractAuditListener {private static final Logger LOG LoggerFactory.getLogger(AuditEventListener.class);Autowiredprivate AuditEventRepository auditEventRepository;Overrideprotected void onAuditEvent(AuditEvent event) {LOG.info(On audit event: timestamp: {}, principal: {}, type: {}, data: {},event.getTimestamp(),event.getPrincipal(),event.getType(),event.getData());auditEventRepository.add(event);}
}与事件发布者发布自己的审核事件 在下面的示例中使用了应用程序事件发布者 org.springframework.context.ApplicationEventPublisher 来发布类型为CUSTOM_AUDIT_EVENT的自定义审核事件。 新的侦听器方法仅侦听那些新事件而先前的方法将忽略它们请注意这只是一个示例。 与其他事件一样自定义事件将使用审核事件存储库进行存储。 Component
public class AuditApplicationEventListener {private static final Logger LOG LoggerFactory.getLogger(AuditApplicationEventListener.class);Autowiredprivate ApplicationEventPublisher applicationEventPublisher;EventListener(condition #event.auditEvent.type ! CUSTOM_AUDIT_EVENT)Asyncpublic void onAuditEvent(AuditApplicationEvent event) {AuditEvent actualAuditEvent event.getAuditEvent();LOG.info(On audit application event: timestamp: {}, principal: {}, type: {}, data: {},actualAuditEvent.getTimestamp(),actualAuditEvent.getPrincipal(),actualAuditEvent.getType(),actualAuditEvent.getData());applicationEventPublisher.publishEvent(new AuditApplicationEvent(new AuditEvent(actualAuditEvent.getPrincipal(), CUSTOM_AUDIT_EVENT)));}EventListener(condition #event.auditEvent.type CUSTOM_AUDIT_EVENT)public void onCustomAuditEvent(AuditApplicationEvent event) {LOG.info(Handling custom audit event ...);}
}注意示例代码 可以在spring-boot-thymeleaf存储库中找到本文的示例代码。 默认情况下两个配置文件均禁用安全性。 通过更改application.properties的security.basic.enabled属性来启用它。 翻译自: https://www.javacodegeeks.com/2017/03/spring-boot-security-events-actuator.htmlspring启动执行