springboot actuator 配置安全



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
http://localhost:8080/actuator
http://localhost:8080/actuator/info
http://localhost:8080/actuator/health

有敏感數據的接口可以自己測試一下:

/autoconfig
/conditions
/configprops
/beans
/heapdump
/threaddump
/env
/info
/mappings
/metrics
/trace

參考

https://xz.aliyun.com/t/2233
https://www.freebuf.com/news/193509.html
https://blog.csdn.net/qq_29668759/article/details/98672900


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM