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