Spring Boot Actuator 是什么
// 官方文档
An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.
比较抽象,大概翻译一下就是可以借助于 Spring Boot Actuator 来对 Spring Boot 应用的健康状态、环境配置、Metrics、Trace、Spring 上下文等信息进行查看,除了一系列查看功能之外,它还实现了 Spring Boot 应用的上下线和内存 dump 功能
Quick Start
引入依赖
// spring-boot-starter-actuator 在不同版本 Spring Boot 中有一定的配置差异,本文采用的是目前最新的 2.4.4 版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.4.4</version>
</dependency>
endpoint
endpoint 是我们使用 Spring Boot Actuator 最需要关心的对象,列举一些常用的 endpoint
ID | Description |
---|---|
beans | 查看 Spring 容器中的所有对象 |
configprops | 查看被 @ConfigurationProperties 修饰的对象列表 |
env | 查看 application.yaml 配置的环境配置信息 |
health | 健康检查端点 |
info | 应用信息 |
metrics | 统计信息 |
mappings | 服务契约 @RequestMapping 相关的端点 |
shutdown | 优雅下线 |
例如 health,只需要访问如下 endpoint 即可获取应用的状态
curl "localhost:8080/actuator/health"
endpoint 的 enable 和 exposure 状态
Spring Boot Actuator 针对于所有 endpoint 都提供了两种状态的配置
- enabled 启用状态。默认情况下除了 shutdown 之外,其他 endpoint 都是启用状态。这也很好理解,其他 endpoint 基本都是查看行为,shutdown 却会影响应用的运行状态。
- exposure 暴露状态。endpoint 的 enabled 设置为 true 后,还需要暴露一次,才能够被访问,默认情况下只有 health 和 info 是暴露的。enabled 不启用时,相关的 endpoint 的代码完全不会被 Spring 上下文加载,所以 enabled 为 false 时,exposure 配置了也无济于事。
/**
*几个典型的配置示例如下
*/
// 启用并暴露所有 endpoint
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
shutdown:
enabled: true
// 只启用并暴露指定 endpoint
management:
endpoints:
enabled-by-default: false
endpoint:
info:
enabled: true
endpoints:
web:
exposure:
include: "info"
// 禁用所有 endpoint
management:
endpoints:
enabled-by-default: false
Spring Boot Actuator 的安全风险
Spring Boot Actuator 提供的一些 endpoint 是会将应用重要的信息暴露出去的,以 env 为例来查看典型的 application.yaml 的示例
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: test
password: 123456
liuhuan:
ak: 123456
sk: 123456
management:
endpoints:
web:
exposure:
include: "*"
我们看看访问 localhost:8080/actuator/env 之后的返回值
{
"activeProfiles": [],
"propertySources": [
{
"name": "server.ports",
"properties": {
"local.server.port": {
"value": 8080
}
}
},
{
"name": "Config resource 'class path resource [application.yaml]' via location 'optional:classpath:/'",
"properties": {
"server.port": {
"value": 8080,
"origin": "class path resource [application.yaml] - 2:9"
},
"spring.datasource.url": {
"value": "jdbc:mysql://localhost:3306/test",
"origin": "class path resource [application.yaml] - 5:44"
},
"spring.datasource.username": {
"value": "test",
"origin": "class path resource [application.yaml] - 6:15"
},
"spring.datasource.password": {
"value": "******",
"origin": "class path resource [application.yaml] - 7:15"
},
"liuhuan.ak": {
"value": "123456",
"origin": "class path resource [application.yaml] - 10:7"
},
"liuhuan.sk": {
"value": "123456",
"origin": "class path resource [application.yaml] - 11:7"
},
"management.endpoints.web.exposure.include": {
"value": "*",
"origin": "class path resource [application.yaml] - 17:18"
}
}
}
]
}
可以发现,对于内置的敏感配置信息 spring.datasource.password,Spring Boot Actuator 是进行了脱敏的,但是对于自定义的一些敏感配置,如 liuhuan.ak 和 liuhuan.sk 却被暴露出来了,在生产服务我们的机器都部署内网,并且一般都是通过反向代理对外暴露的服务,这类 endpoint 是不会被外部用户访问到的。但还是会有些情况会导致安全漏洞,例如:
- 反向代理误配置了根节点,将 actuator 的 endpoint 和 web 服务一起暴露了出去
- 线上配置没问题,测试环境部署时开通了公网 SLB,导致 actuator 的 endpoint 暴露了出去
- 同一环境中某台机器被攻陷,导致应用配置信息泄露
安全建议
- 最小粒度暴露 endpoint。只开启并暴露真正用到的 endpoint,而不是配置:management.endpoints.web.exposure.include=*
- 为 endpoint 配置独立的访问端口,从而和 web 服务的端口分离开,避免暴露 web 服务时,误将 actuator 的 endpoint 也暴露出去。例:management.port=8099
- 引入 spring-boot-starter-security 依赖,为 actuator 的 endpoint 配置访问控制
- 慎重评估是否需要引入 spring-boot-stater-actuator