执行器的Spring启动和安全性事件
Spring Boot Actuator提供了审核功能,用于在启用Spring Security的Spring Boot应用程序中发布和侦听与安全相关的事件。 默认事件是身份验证成功,身份验证失败和访问被拒绝,但是可以使用自定义事件进行扩展。
确保在项目中启用了Spring Boot Security和Actuator
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
执行器
默认情况下, /auditevents
端点/auditevents
启用状态,因此在启动应用程序(并使用应用程序日志中提供的用户名user
和password登录)后,您可以看到当前的安全事件:
{"events": [{"timestamp": "2017-03-14T22:59:58+0000","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:07+0000","principal": "user","type": "AUTHENTICATION_SUCCESS","data": {"details": {"remoteAddress": "0:0:0:0:0:0:0:1","sessionId": null}}}]
}
/auditevents
端点接受请求的可选参数:
pricipal
-主体名称after
–事件发生后的日期,格式如下:yyyy-MM-dd'T'HH:mm:ssZ
type
–事件类型(例如AUTHORIZATION_FAILURE,AUTHENTICATION_SUCCESS,AUTHENTICATION_FAILURE,AUTHENTICATION_SWITCH)
请求示例:
http:// localhost:8080 / auditevents?type = AUTHORIZATION_FAILURE&after = 2017-03-14T23%3A14%3A12%2B0000&principal = 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: {details=org.springframework.security.web.authentication.WebAuthenticationDetails@b364: 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);@EventListener@Asyncpublic 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.html
执行器的Spring启动和安全性事件相关推荐
- spring启动执行_执行器的Spring启动和安全性事件
spring启动执行 Spring Boot Actuator提供了审核功能,用于在启用了Spring Security的Spring Boot应用程序中发布和侦听与安全相关的事件. 默认事件是身份验 ...
- spring 启动完成后事件监听器处理
有时候我们在spring容器启动完成后,我们需要做一些处理动作,这个时候怎么做呢? spring提供了事件监听器的处理机制. spring提供了内置的几类的事件: ContextClosedEvent ...
- Spring5源码解析-Spring中的异步事件
上一篇 Spring框架中的事件和监听器并未对Spring框架中的异步事件涉及太多,所以本篇是对其一个补充. 同步事件有一个主要缺点:它们在所调用线程的本地执行(也就是将所调用线程看成主线程的话,就是 ...
- 了解这些,你就可以在Spring启动时为所欲为了
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! Spring 是一个控制反转依赖管理的容器,作为 Java ...
- Spring 与 Spring Boot 中的事件机制
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 温安适 来源 | https://my.osc ...
- kafka 启动_「首席看Event Hub」如何在您的Spring启动应用程序中使用Kafka
在体系结构规划期间选择正确的消息传递系统始终是一个挑战,但这是需要确定的最重要的考虑因素之一.作为一名开发人员,我每天都要编写需要服务大量用户并实时处理大量数据的应用程序. 通常,我将Java与Spr ...
- spring 启动进度_在Web浏览器中显示Spring应用程序启动的进度
spring 启动进度 重新启动企业应用程序时,客户打开Web浏览器时会看到什么? 他们什么也没看到,服务器还没有响应,因此Web浏览器显示ERR_CONNECTION_REFUSED 应用程序前面的 ...
- spring启动流程(一):启动流程概览
1. spring 启动流程概览 在前面 demo 的 main()方法中,有这么一行: ApplicationContext context =new AnnotationConfigApplica ...
- SpringBoot启动发生的事件顺序
Springboot启动过程发生事件 ApplicationStartingEvent 应用开始启动 ApplicationEnvironmentPreparedEvent:环境准备完成(contex ...
最新文章
- 脑电分析系列[MNE-Python-5]| Python机器学习算法随机森林判断睡眠类型
- 为什么matlab显示error,【求救】我安装了资源 MATLAB R2012b 后,显示有error……
- 20145101 《Java程序设计》第7周学习总结
- Linux IO调度器相关算法介绍
- DE21 Convolution Formula
- 38张技术知识图谱,IT架构师必备【附下载】
- 数据结构实训——哈希表设计
- Android平台车牌识别SDK
- 开机加速——在注册表里禁止开机自检硬盘
- Android跳转应用市场更新自己《完美适配大部分手机应用商店》
- 云桌面到底是干什么的?
- 【附源码】计算机毕业设计java医院人事及科室病区管理设计与实现
- 吴恩达Course1《神经网络与深度学习》week2:神经网络基础 测验及作业
- 论文阅读:FASTEMIT: LOW-LATENCY STREAMING ASR WITH SEQUENCE-LEVEL EMISSION REGULARIZATION
- mac电脑升级Monterey12.1版之后L2TP连接公司内网后无法正常访问的问题解决
- java计时器脚本_在倒数计时器脚本中跳过很多秒
- ICC Floorplan遇到的坑以及解决方法
- vlc多媒体播放器VLC Media Player 3.0.7.1中文版
- 如何打造一个低效能技术团队?
- Flume之Failover和Load balancing原理及实例
热门文章
- 一道非常棘手的 Java 面试题:i++ 是线程安全的吗
- 阿里巴巴对Java编程【常量定义】的规约
- 【最全最详细】publiccms其他常用代码片段(内容、站点)
- JSP 获得服务器时间和浏览器时间
- php array分组,php数组分组简单例子
- docker export_docker使用简介
- windows查找端口占用并杀死端口进程
- tomcat(supplement)HttpConnector.initialize() 和 start() 方法 以及 StandardContext.start()方法的分析
- 反射和配置文件的实际应用
- 赞扬别人团建评论_赞扬精心设计:基于属性的测试如何帮助我成为更好的开发人员...