本文将介绍如何启用Spring Boot Actuator的所有Endpoints。首先从maven依赖开始,然后讲解如何通过配置文件来控制Endpoint(后称作端点)。最后再学习一下如何确保端点的安全。
其中Spring Boot 1.x和Spring Boot 2.x在Actuator的端点配置上会有一定的区别。当出现区别时,会进行提示。
要使用Spring Boot Actuator需要先在项目中引入对应的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.4.3</version>
</dependency>
复制代码
从Spring Boot 2.x开始,如果想通过HTTP的方式进行访问,还需要引入web starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
复制代码
从Spring Boot 2.x开始,我们需要手动的去启用和暴露端点。默认情况下,除了/shutdown之外的所有端点都是可用的,同时只有/health和/info端点是对外暴露的。即便我们为应用程序配置了多个根上下文,所有的端点都可以通过/actuator来进行查找。
这意味着,一旦我们引入了合适的starter到maven配置中,我们便可以通过http://localhost:8080/actuator/health和http://localhost:8080/actuator/info来进行两个端点的访问。
访问http://localhost:8080/actuator,返回可用的端点列表如下:
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}
复制代码
下面通过配置来暴露除了/shutdown之外的所有端点,在application.properties中进行如下配置:
management.endpoints.web.exposure.include=*
复制代码
重启,再次访问/actuator,可以看到除了/shutdown之外的其他所有端点。
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},
"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false},
"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},
"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},
"env":{"href":"http://localhost:8080/actuator/env","templated":false},
"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},
"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},
"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},
"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},
"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},
"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
复制代码
一些端点可能会暴露敏感数据,所以我们需要知道如何细粒度去暴露端点。
management.endpoints.web.exposure.include属性可以通过逗号分隔符来配置多个端点,比如我们暴露/beans和/loggers端点:
management.endpoints.web.exposure.include=beans, loggers
复制代码
除了包含具体端点之外,我们还可以排除一些端点。比如暴露除了/threaddump之外的所有端点:
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=threaddump
复制代码
include和exclude属性均支持配置一个端点列表。exclude属性的优先级要高于include。
下面来看一下如何细粒度的开启指定的端点。首先,需要关闭默认开启的所有端点:
management.endpoints.enabled-by-default=false
复制代码
开启并暴露/health端点:
management.endpoint.health.enabled=true
management.endpoints.web.exposure.include=health
复制代码
通过上述配置,便可以访问/health端点了。
因为/shutdown端点比较敏感的原因,该端点默认是不可用的。可在application.properties文件中通过如下配置进行启动:
management.endpoint.shutdown.enabled=true
复制代码
此时,访问/actuator端点,就可以在列表中看到它了。/shutdown端点仅接收POST请求,可以通过如下方式优雅的关闭服务:
curl -X POST http://localhost:8080/actuator/shutdown
复制代码
在真实的应用中,我们通常需要对应用程序进行安全防护。基于此,我们需要确保Actuator端点的安全。
首先,在应用程序中添加security的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.4.4</version>
</dependency>
复制代码
为了最基本的安全保障,这是我们需要做的事。添加security starter依赖之后,程序会自动将身份验证应用于除/info和/health之外的所有公开端点。
下面,自定义security以将 /actuator 端点限制为 ADMIN 角色。
首先排除默认的安全配置:
@SpringBootApplication(exclude = {
SecurityAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class
})
复制代码
留意下ManagementWebSecurityAutoConfiguration.class类,它会让安全配置应用到 /actuator。
在我们的配置类中,配置几个用户和角色,同时有一个ADMIN的角色可以使用:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth
.inMemoryAuthentication()
.withUser("user")
.password(encoder.encode("password"))
.roles("USER")
.and()
.withUser("admin")
.password(encoder.encode("admin"))
.roles("USER", "ADMIN");
}
复制代码
SpringBoot 为我们提供了一个方便的请求匹配器,用于我们的Actuator端点。利用它可以将/actuator锁定为ADMIN角色:
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) -> requests.anyRequest().hasRole("ADMIN"));
复制代码
通过本文,我们学习了Spring Boot是如何来默认配置Actuator的。随后,我们在应用程序的application.properties文件中定义了端点的启用、禁用和暴露。鉴于Spring Boot对/shutdwon端点的不同处理,我们学习了如何单独启用该端点。最后,学习了如何基于security对端点来进行安全防护。
源码地址:github.com/eugenp/tuto… 。
博主简介:《SpringBoot技术内幕》技术图书作者,酷爱钻研技术,写技术干货文章。公众号:「程序新视界」,博主的公众号,欢迎关注~
技术交流:请联系博主微信号:zhuan2quan
原网址: 访问
创建于: 2023-06-07 09:55:33
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
java windows火焰图_mob64ca12ec8020的技术博客_51CTO博客 - 在windows下不可行,不知道作者是怎样搞的 监听SpringBoot 服务启动成功事件并打印信息_监听springboot启动完毕-CSDN博客 SpringBoot中就绪探针和存活探针_management.endpoint.health.probes.enabled-CSDN博客 u2u转换板 - 嘉立创EDA开源硬件平台 Spring Boot 项目的轻量级 HTTP 客户端 retrofit 框架,快来试试它!_Java精选-CSDN博客 手把手教你打造一套最牛的知识笔记管理系统! - 知乎 - 想法有重合-理论可参考 安宇雨 闲鱼 机械键盘 客制化 开贴记录 文本 linux 使用find命令查找包含某字符串的文件_beijihukk的博客-CSDN博客_find 查找字符串 ---- mac 也适用 安宇雨 打字音 记录集合 B站 bilibili 自行搭建 开坑 真正的客制化 安宇雨 黑苹果开坑 查找工具包maven pom 引用地 工具网站 Dantelis 介绍的玩轴入坑攻略 --- 关于轴的一些说法 --- 非官方 ---- 心得而已 --- 长期开坑更新 [本人问题][新开坑位]关于自动化测试的工具与平台应用 机械键盘 开团 网站记录 -- 能做一个收集的程序就好了 不过现在没时间 -- 信息大多是在群里发的 - 你要让垃圾佬 都去一个地方看难度也是很大的 精神支柱 [超级前台]sprinbboot maven superdesk-app 记录 [信息有用] [环境准备] [基本完成] [sebp/elk] 给已创建的Docker容器增加新的端口映射 - qq_30599553的博客 - CSDN博客 [正在研究] Elasticsearch, Logstash, Kibana (ELK) Docker image documentation elasticsearch centos 安装记录 及 启动手记 正式服务器 39 elasticsearch 问题合集 不断更新 6.1.1 | 6.5.1 两个版本 博客程序 - 测试 - bug记录 等等问题 laravel的启动过程解析 - lpfuture - 博客园 OAuth2 Server PHP 用 Laravel 搭建带 OAuth2 验证的 RESTful 服务 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法 - 煮茶的博客 - SegmentFault 思否 使用 OAuth2-Server-php 搭建 OAuth2 Server - 午时的海 - 博客园 基于PHP构建OAuth 2.0 服务端 认证平台 - Endv - 博客园 Laravel 的 Artisan 命令行工具 Laravel 的文件系统和云存储功能集成 浅谈Chromium中的设计模式--终--Observer模式 浅谈Chromium中的设计模式--二--pre/post和Delegate模式 浅谈Chromium中的设计模式--一--Chromium中模块分层和进程模型 DeepMind 4 Hacking Yourself README.md update 20211011
Laravel China 简书 知乎 博客园 CSDN博客 开源中国 Go Further Ryan是菜鸟 | LNMP技术栈笔记 云栖社区-阿里云 Netflix技术博客 Techie Delight Linkedin技术博客 Dropbox技术博客 Facebook技术博客 淘宝中间件团队 美团技术博客 360技术博客 古巷博客 - 一个专注于分享的不正常博客 软件测试知识传播 - 测试窝 有赞技术团队 阮一峰 语雀 静觅丨崔庆才的个人博客 软件测试从业者综合能力提升 - isTester IBM Java 开发 使用开放 Java 生态系统开发现代应用程序 pengdai 一个强大的博主 HTML5资源教程 | 分享HTML5开发资源和开发教程 蘑菇博客 - 专注于技术分享的博客平台 个人博客-leapMie 流星007 CSDN博客 - 舍其小伙伴 稀土掘金 Go 技术论坛 | Golang / Go 语言中国知识社区
最新评论