詳解配置spring-boot-actuator時候遇到的一些小問題
前言
spring-boot-actuator是一個spring-boot提供的用于監(jiān)控組件,只需要在代碼中加入依賴就可以了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
遇到的一些小問題
1.可以加入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
來保證actuator暴露接口的安全性,可以通過 -u 'user:password' 方式來訪問basic auth
2.如果項目依賴的是springmvc框架,并且基礎(chǔ)的配置文件是 application.yaml的話,可以增加 application.properties 文件來配置安全性的配置.
3.如果加入了security依賴,則所有的接口默認都需要被驗證,如果只想 /admin路徑下的請求進行驗證,則需要加入配置
security.basic.enabled=true security.basic.path=/admin security.user.name=admin security.user.password=password
4.如果項目依賴的是非springmvc框架的話, 需要在依賴中加入mvc的依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency>
5.如果management.security.enabled的值是false的話,除開health接口還依賴endpoints.health.sensitive的配置外,其他接口都不需要輸入用戶名和密碼了。
6.actuator暴露的health接口權(quán)限是由兩個配置: management.security.enabled 和 endpoints.health.sensitive組合的結(jié)果進行返回的。
| management.security.enabled | endpoints.health.sensitive | Unauthenticated | Authenticated |
| false | false | Full content | Full content |
| false | true | Status only | Full content |
| true | false | Status only | Full content |
| true | true | No content | Full content |
7.actuator組件里面除開上面提到的metrics和health接口以外,還有很多默認的其他接口,如果它默認的接口不能滿足你的需求的話,還可以通過繼承它的 AbstractEndpoint 類來實現(xiàn)自己的Endpoint
最后附加一個配置文件例子:
security.basic.enabled=true security.basic.path=/admin #針對/admin路徑進行認證 security.user.name=admin #認證使用的用戶名 security.user.password=password #認證使用的密碼 management.security.roles=SUPERUSER management.port=11111 #actuator暴露接口使用的端口,為了和api接口使用的端口進行分離 management.context-path=/admin #actuator暴露接口的前綴 management.security.enabled=true #actuator是否需要安全保證 endpoints.metrics.sensitive=false #actuator的metrics接口是否需要安全保證 endpoints.metrics.enabled=true endpoints.health.sensitive=false #actuator的health接口是否需要安全保證 endpoints.health.enabled=true
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot集成SpringFox 3.0與Pageable參數(shù)處理方法
這篇文章主要介紹了Spring Boot集成SpringFox 3.0與Pageable參數(shù)處理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10
springboot jsp支持以及轉(zhuǎn)發(fā)配置方式
文章介紹了如何在Spring Boot項目中配置和使用JSP,并提供了一步一步的指導,包括添加依賴、配置文件設(shè)置、控制器和視圖的使用2024-12-12
Java設(shè)計模式之模板模式(Template模式)介紹
這篇文章主要介紹了Java設(shè)計模式之模板模式(Template模式)介紹,定義一個操作中算法的骨架,將一些步驟的執(zhí)行延遲到其子類中,需要的朋友可以參考下2015-03-03
mybatisPlus條件構(gòu)造器常用方法小結(jié)
這篇文章主要介紹了mybatisPlus條件構(gòu)造器常用方法,首先是.select和其他條件,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10

