执行器的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.propertiessecurity.basic.enabled属性来启用它。

翻译自: https://www.javacodegeeks.com/2017/03/spring-boot-security-events-actuator.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/350860.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

docker 查看日志_8个优秀Docker容器监控工具,收藏了

Docker是目前使用最广泛的容器之一&#xff0c;但它并不总是像物理硬件一样可见。而使用docker容器监控工具有助于消除这种透明度的缺失。以下介绍8种优秀Docker容器监控工具。1.SolarWinds Server&Application MonitorSolarWinds Server&Application Monitor是一个应用…

ubuntu 打包压缩

打包 tar -cvf test.tar *.txt tar -cvf test.tar *.txt 解包 tar -xvf test.tar tar -xvf test.tar -C abc/ 解压到当前路径的abc目录下 打包压缩 tar -zcvf test.tar.gz *.txt 解压缩包 tar -zxvf test.tar -C abc/ 解压到当前路径的abc目录下 .tar.bz2格式…

计算机基础知识:什么是位、字节、字、KB、MB

位、字节、字、KB、MB 位&#xff1a;“位(bit)”是电子计算机中最小的数据单位。每一位的状态只能是0或1。 字节&#xff1a;8个二进制位构成1个“字节(Byte)”&#xff0c;它是存储空间的基本计量单位。1个字节可以储存1个英文字母或者半个汉字&#xff0c;换句话说&#xf…

Tr A(矩阵快速幂)

A为一个方阵&#xff0c;则Tr A表示A的迹&#xff08;就是主对角线上各项的和&#xff09;&#xff0c;现要求Tr(A^k)%9973。 Input 数据的第一行是一个T&#xff0c;表示有T组数据。 每组数据的第一行有n(2 < n < 10)和k(2 < k < 10^9)两个数据。接下来有n行&am…

matlab中的方波信号图片_基于Matlab的雷达信号处理仿真

这是一个较为基础的图文教程(含仿真代码)&#xff1a;利用MATLAB设计经典的雷达数字信号处理。该系统具备对雷达目标回波的处理能力&#xff0c;能够从噪声中将目标检测出来&#xff0c;并提取目标的距离、速度、角度信息。教程分五节完成&#xff0c;主要包括&#xff1a;第一…

ubuntu find

find ./ -name “*.txt” find ./ -size 1M (当前目录下大小为1M) sudo find ./ -size 1M &#xff08;大小为1M&#xff0c;sudo为获取权限&#xff09; sudo find ./ -size 1M &#xff08;大于为1M&#xff09; sudo find ./ -size -1M &#xff08;小于1M&#xff09…

git clone 一些简单笔记

自使用了git后&#xff0c;就彻底喜欢上了&#xff0c;深深体会到了自由的感觉&#xff0c;记录一些简单的笔记和使用心得&#xff0c;仅供留迹&#xff0c;以备后查。。。 git clone 命令参数&#xff1a; usage: git clone [options] [--] <repo> [<dir>]-v, --v…

github上java_GitHub上Java的Bloom Bloom实现

github上java布隆过滤器是集数据结构的一种 。 对于那些不了解的对象&#xff0c;“设置数据结构”仅包含一个主要方法。 它仅用于确定特定元素是否包含在一组元素中。 大多数数据结构&#xff08;例如Hash Map &#xff0c; Linked List或Array &#xff09;都可以相当容易地创…

Error:The supplied javaHome seems to be invalid. I cannot find the java executable

设置一下gradle的JVM路径就好 转载于:https://www.cnblogs.com/shimu/p/10708888.html

Linux启动nacos成功日志_微服务系列之Nacos配置中心

Nacos 介绍Nacos 是 Alibaba 公司推出的开源工具&#xff0c;用于实现分布式系统的服务发现与配置管理。英文全称 Dynamic Naming and Configuration Service&#xff0c;Na 为 Naming/NameServer 即注册中心&#xff0c;co 为 Configuration 即配置中心&#xff0c;Service 是…

ubuntu 常用快捷键、常用命令

ls 显示当前文件夹下文件 pwd 显示当前目录 history 显示以前命令 touch 创建文件 例子&#xff1a; touch 1.txt &#xff1b; touch1.py等等 mkdir 创建文件 例子&#xff1a;mkdir abc&#xff1b; mkdir abc/d; mkdir abc/123/a -p …

Apache Camel 2.19发布–新增功能

Apache Camel 2.19于2017年5月5日发布&#xff0c;大约在一段时间后&#xff0c;我做了一个小博客&#xff0c;介绍了该版本包含的值得注意的新功能和改进。 这是值得注意的新功能和改进的列表。 1. Spring Boot的改进 Camel 2.19版本已针对Spring Boot进行了许多改进。 例如…

Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置

两年半前写的关于Maven的介绍&#xff0c;现在看来都还是不错的&#xff0c;自己转下。写博客的一大好处就是方便自己以后查阅&#xff0c;自己总结的总是最靠谱的。 由浅入深&#xff0c;主要介绍maven的用途、核心概念(Pom、Repositories、Artifact、Build Lifecycle、Goal)、…

C#枚举(Enum)小结

枚举概念 枚举类型&#xff08;也称为枚举&#xff09;提供了一种有效的方式来定义可能分配给变量的一组已命名整数常量。该类型使用enum关键字声明。 示例代码1 enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; 默认情况下枚举中每个元素的基本…

用js实现导航栏shoufang效果_【读者投稿】用Github+docsify,我花了半天就搭好了个人博客...

前言“作为一个真正的码农&#xff0c;不能没有自己的个人博客”&#xff0c;这是我说的。惭愧的是&#xff0c;入行两年多了都没搞起来&#xff0c;这让我一度怀疑自己是个假程序员。昨天终于克服了心里的“犹豫”和“恐惧”&#xff0c;尝试搭建了一把&#xff0c;半天就搞好…

ubuntu 重定向

重定向 &#xff08;重新设定方向&#xff09; ls > xxx.txt 将ls显示的内容写入到xxx.txt ls -lah > xxx.txt ls >> xxx.txt 将ls显示的内容以追加方式写入到xxx.txt

IDEA快捷键及xml文件中网址报错

AltShiftTab 切换窗口(从后往前) divTab    补全为 <div></div> #boxTab   补全为 <div id"box"></div> .boxTab   补全为 <div class"box"></div> …

python随机生成数字列表_详解Python利用random生成一个列表内的随机数

首先,需要导入random模块: import random 随机取1-33之间的1个随机数&#xff0c;可能重复: random.choice(range(1,34)) print得到一系列随机数,执行一次得到一个随机数: print(random.choice(range(1,34))) 随机取1-33之间的6个随机数&#xff0c;可能重复: random.choices(r…

构建Java Web开发环境

1.1 JDK&#xff08;Java Development Kit&#xff09;的安装与配置1.1.1 下载最新的JDK&#xff08;我下载的是Java SE 6 Update 25&#xff09;下载地址&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/index.html1.1.2 JDK的安装1、双击名为“jdk-6u2…

ubuntu cd 改变路径

. 表示当前路径 cd &#xff0e;&#xff0e; 后退到上一层路径 cd ../.. 调到上上层路径,相对路径 cd /home/hjd/ 绝对路径 cd ~ 回到家目录 &#xff08;/home/hjd/&#xff09;