springboot actuator 配置安全
- 2020 年 4 月 13 日
- 筆記
springboot actuator監控是什麼?類似php的phpinfor()函數,不過actuator更強大,可以查看的數據、狀態更多。Actuator是Spring Boot提供的對應用系統的監控和管理的集成功能,可以查看應用配置的詳細資訊,例如自動化配置資訊、創建的Spring beans資訊、系統環境變數的配置信以及Web請求的詳細資訊等。如果使用不當或者一些不經意的疏忽,可能造成資訊泄露
等嚴重的安全隱患。
使用
pom加入依賴
<!--actuator模組為SpringBoot提供一系列用於監控的端點-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
pom加入依賴(安全版)
<dependencies>
<!-- 如果使用http調用的方式,還需要這個依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 必須的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 安全需要,為了保證actuator暴露的監控介面的安全性,需要添加安全控制的依賴spring-boot-start-security依賴,訪問應用監控端點時,都需要輸入驗證資訊。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
properties配置文件
在application.properties核心配置文件中除了定義數據源外,還需要添加 management.security.enabled=false 配置。
不加的話,訪問監控路徑會報401。
#########################################################
### Actuator Monitor -- Actuator configuration ###
#########################################################
management.security.enabled=false
在SpringBoot的application.yml配置文件中加入
# ============================= actuator監控 ============================= #
management:
server:
port: 1234 # 管理的埠調整成1234
address: 127.0.0.1 # 只允許127.0.0.1訪問
servlet:
context-path: /monitor # actuator的訪問路徑
endpoint:
shutdown:
enabled: true # 啟用shutdown功能
beans.cache.time-to-live: 10s
env.enabled: true # 啟用端點 env
endpoints:
enabled-by-default: true # 設置端點是否可用 默認只有shutdown可用
web:
# 設置是否暴露端點 默認只有health和info可見
exposure:
# include: env # 方式1: 暴露端點 env 配置多個,隔開
include: "*" # 方式2: 包括所有端點, 注意需要添加引號
# 排除端點
exclude: shutdown
注意:若在核心配置文件中未添加 management.security.enabled=false 配置,將會導致用戶在訪問部分監控地址時訪問受限,報401
未授權錯誤。
常見的監控項目
方法 | 路徑 | 描述 |
---|---|---|
GET | /autoconfig | 查看自動配置的使用情況 |
GET | /conditions | 提供了一份自動配置報告,記錄哪些自動配置條件通過了,哪些沒通過 |
GET | /configprops | 描述配置屬性(包含默認值)如何注入Bean |
GET | /beans | 描述應用程式上下文里全部的Bean,以及它們的關係 |
GET | /dump | 列印執行緒棧 |
GET | /heapdump | 獲取堆的快照 |
GET | /threaddump | 獲取執行緒活動的快照 |
GET | /env | 獲取全部環境屬性 |
GET | /env/{name} | 根據名稱獲取特定的環境屬性值 |
GET | /health | 報告應用程式的健康指標,這些值由HealthIndicator的實現類提供 |
GET | /info | 獲取應用程式的訂製資訊,這些資訊由info打頭的屬性提供 |
GET | /mappings | 描述全部的URI路徑,以及它們和控制器(包含Actuator端點)的映射關係 |
GET | /metrics | 報告各種應用程式度量資訊,比如記憶體用量和HTTP請求計數 |
GET | /metrics/{name} | 報告指定名稱的應用程式度量值 |
POST | /shutdown | 關閉應用程式,要求endpoints.shutdown.enabled設置為true |
GET | /trace | 提供基本的HTTP請求跟蹤資訊(時間戳、HTTP頭等) |
加入了依賴之後,在外部是可以訪問到如下路徑的:
# 加入上述依賴後,默認可以訪問的url
//localhost:8080/actuator
//localhost:8080/actuator/info
//localhost:8080/actuator/health
有敏感數據的介面可以自己測試一下:
/autoconfig
/conditions
/configprops
/beans
/heapdump
/threaddump
/env
/info
/mappings
/metrics
/trace
參考
//xz.aliyun.com/t/2233
//www.freebuf.com/news/193509.html
//blog.csdn.net/qq_29668759/article/details/98672900