基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式
概述
微服務(wù)作為一項(xiàng)在云中部署應(yīng)用和服務(wù)的新技術(shù)是當(dāng)下比較熱門話題,而微服務(wù)的特點(diǎn)決定了功能模塊的部署是分布式的,運(yùn)行在不同的機(jī)器上相互通過服務(wù)調(diào)用進(jìn)行交互,業(yè)務(wù)流會經(jīng)過多個微服務(wù)的處理和傳遞,在這種框架下,微服務(wù)的監(jiān)控顯得尤為重要。
而Actuator正是Spring Boot提供的對應(yīng)用系統(tǒng)的監(jiān)控和管理的集成功能,可以查看應(yīng)用配置的詳細(xì)信息,例如自動化配置信息、創(chuàng)建的Spring beans信息、系統(tǒng)環(huán)境變量的配置信息以及Web請求的詳細(xì)信息等。
如果使用不當(dāng)或者一些不經(jīng)意的疏忽,可能造成信息泄露等嚴(yán)重的安全隱患。
Actuator使用
Actuator應(yīng)用監(jiān)控使用只需要添加spring-boot-starter-actuator依賴即可,如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
可以在application.properties中指定actuator的訪問端口、訪問路徑等信息:
# 訪問示例:http://localhost:9595/monitor
management:
endpoints:
web:
# actuator的訪問路徑,替換默認(rèn)/actuator
base-path: /monitor
# 設(shè)置是否暴露端點(diǎn) 默認(rèn)只有health和info可見
exposure:
# include: env # 方式1: 暴露端點(diǎn)env,配置多個以,隔開
include: "*" # 方式2: 包括所有端點(diǎn),注意需要添加引號
# 排除端點(diǎn)
exclude: shutdown
server:
port: 9595 #新開監(jiān)控端口,不和應(yīng)用用同一個端口
endpoint:
health:
show-details: always # 顯示db、redis、rabbti連接情況等
shutdown:
enabled: true #默認(rèn)情況下,除shutdown以外的所有端點(diǎn)均已啟用。手動開啟
此時,運(yùn)行示例,訪問/monitor/即可查看所有端點(diǎn)信息,再訪問/monitor/env即可查看該應(yīng)用全部環(huán)境屬性,如圖:


Endpoints(端點(diǎn))介紹
Endpoints 是 Actuator 的核心部分,它用來監(jiān)視應(yīng)用程序及交互,spring-boot-actuator中已經(jīng)內(nèi)置了非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同時也允許我們擴(kuò)展自己的端點(diǎn)。
Endpoints 分成兩類:原生端點(diǎn)和用戶自定義端點(diǎn);自定義端點(diǎn)主要是指擴(kuò)展性,用戶可以根據(jù)自己的實(shí)際應(yīng)用,定義一些比較關(guān)心的指標(biāo),在運(yùn)行期進(jìn)行監(jiān)控。
原生端點(diǎn)是在應(yīng)用程序里提供的眾多 restful api 接口,通過它們可以監(jiān)控應(yīng)用程序運(yùn)行時的內(nèi)部狀況。原生端點(diǎn)又可以分成三類:
- 應(yīng)用配置類:可以查看應(yīng)用在運(yùn)行期間的靜態(tài)信息:例如自動配置信息、加載的spring bean信息、yml文件配置信息、環(huán)境信息、請求映射信息;
- 度量指標(biāo)類:主要是運(yùn)行期間的動態(tài)信息,例如堆棧、請求連、一些健康指標(biāo)、metrics信息等;
- 操作控制類:主要是指shutdown,用戶可以發(fā)送一個請求將應(yīng)用的監(jiān)控功能關(guān)閉。
Actuator 默認(rèn)提供了以下接口,具體如下表所示:
|
ID |
描述 |
默認(rèn)啟用 |
默認(rèn)公開 |
|---|---|---|---|
| auditevents | 公開當(dāng)前應(yīng)用程序的審計事件信息 | Yes | No |
| beans | 顯示應(yīng)用程序中所有Spring bean的完整列表 | Yes | No |
| conditions | 顯示在配置和自動配置類上評估的條件以及它們是否匹配的原因 | Yes | No |
| configprops | 顯示所有@ConfigurationProperties對照的列表 | Yes | No |
| env | 從Spring的ConfigurableEnvironment中公開屬性 | Yes | No |
| flyway | 顯示已應(yīng)用的任何Flyway數(shù)據(jù)庫遷移 | Yes | No |
| health | 顯示應(yīng)用程序健康信息 | Yes | Yes |
| httptrace | 顯示HTTP跟蹤信息(默認(rèn)情況下,最后100個HTTP請求-響應(yīng)交互) | Yes | No |
| info | 顯示任意應(yīng)用程序信息 | Yes | Yes |
| loggers | 顯示和修改應(yīng)用程序中記錄器的配置 | Yes | No |
| liquibase | 顯示已應(yīng)用的任何Liquibase數(shù)據(jù)庫遷移 | Yes | No |
| metrics | 顯示當(dāng)前應(yīng)用程序的“指標(biāo)”信息 | Yes | No |
| mappings | 顯示所有@RequestMapping路徑對照的列表 | Yes | No |
| scheduledtasks | 顯示應(yīng)用程序中調(diào)度的任務(wù) | Yes | No |
| sessions | 允許從Spring Session支持的會話存儲中檢索和刪除用戶會話 | Yes | No |
| shutdown | 讓應(yīng)用程序優(yōu)雅地關(guān)閉 | No | No |
| threaddump | 執(zhí)行線程轉(zhuǎn)儲 | Yes | No |
安全措施
如果上述請求接口不做任何安全限制,安全隱患顯而易見。實(shí)際上Spring Boot也提供了安全限制功能。比如要禁用/env接口,則可設(shè)置如下:
endpoint:
env:
enabled: false
另外也可以引入spring-boot-starter-security依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在application.properties中開啟security功能,配置訪問權(quán)限驗(yàn)證,這時再訪問actuator功能時就會彈出登錄窗口,需要輸入賬號密碼驗(yàn)證后才允許訪問。
spring:
security:
user:
password: 123456
name: jaler
為了只對actuator功能做權(quán)限驗(yàn)證,其他應(yīng)用接口不做認(rèn)證,我們可以重新定制下SpringSecurity
package com.jaler.common.common.config;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
Environment env;
@Override
protected void configure(HttpSecurity security) throws Exception {
String contextPath = env.getProperty("management.endpoints.web.base-path");
if(StringUtils.isEmpty(contextPath)) {
contextPath = "";
}
security.csrf().disable();
security.authorizeRequests()
.antMatchers("/**"+contextPath+"/**")
.authenticated()
.anyRequest()
.permitAll()
.and()
.httpBasic();
}
}
再次訪問http://localhost:9595/monitor,此時需要進(jìn)行權(quán)限驗(yàn)證,如下圖:

安全建議
1.只開放某些無敏感信息的端點(diǎn)。
2.打開安全限制并進(jìn)行身份驗(yàn)證,訪問Actuator接口時需要登錄。
3.Actuator訪問接口使用獨(dú)立端口,并配置不對外網(wǎng)開放。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot集成Actuator監(jiān)控功能詳解
- SpringBoot監(jiān)控模塊Actuator的用法詳解
- SpringBoot2開啟Actuator端點(diǎn)監(jiān)控的方法
- SpringBoot 指標(biāo)監(jiān)控actuator的專題
- springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解
- Springboot actuator應(yīng)用后臺監(jiān)控實(shí)現(xiàn)
- 使用SpringBoot Actuator監(jiān)控應(yīng)用示例
- springboot 使用Spring Boot Actuator監(jiān)控應(yīng)用小結(jié)
- 詳解關(guān)于springboot-actuator監(jiān)控的401無權(quán)限訪問
- SpringBoot Actuator監(jiān)控的項(xiàng)目實(shí)踐
相關(guān)文章
springcloud config配置讀取優(yōu)先級過程詳解
這篇文章主要介紹了springcloud config配置讀取優(yōu)先級過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
Eclipse下基于Java的OpenCV開發(fā)環(huán)境配置教程
這篇文章主要為大家詳細(xì)介紹了Eclipse下基于Java的OpenCV開發(fā)環(huán)境配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
gradle項(xiàng)目中資源文件的相對路徑打包技巧必看
這篇文章主要介紹了gradle項(xiàng)目中資源文件的相對路徑打包技巧必看篇,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

