开发避坑指南(28):Spring Boot端点检查禁用失效解决方案

发布于:2025-08-19 ⋅ 阅读:(20) ⋅ 点赞:(0)

问题

Spring Boot 配置了禁用端点检查,可是某些端点还是能访问?或者干脆禁用失效?是什么问题?

问题背景

Spring Boot 版本:2.0.3

原配置

#关闭Spring Boot端点检查
management.endpoint.health.enabled=false

问题分析

首先,要搞清为什么禁用Spring Boot端点检查失效得先搞清楚Spring Boot的Actuator一共有多少个端点?默认开启了哪些端点?

1、Spring Boot 1.x 版本‌
默认提供 ‌13个核心端点‌,包括:
/autoconfig、/configprops、/beans、/dump、/env、/health、/info、/metrics、/mappings、/shutdown、/trace,以及环境变量和线程相关端点。

2、Spring Boot 2.x/3.x 版本‌
端点数量扩展至 ‌20个以上‌,新增了如:
/auditevents、/caches、/conditions、/flyway、/heapdump、/httpexchanges、/integrationgraph、/liquibase、/scheduledtasks、/sessions、/startup等。

**默认仅暴露 /health 和 /info,其他端点需通过配置显式启用。**可以通过访问端点/actuator查看系统开启了哪些端点,默认的返回结果是3个:

{
    "_links": {
        "self": {
            "href": "http://localhost:8073/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8073/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8073/actuator/info",
            "templated": false
        }
    }
}

解决办法

通过上面分析,先检查配置是否正确,搞清楚是要禁用某个端点还是禁用全部端点。比如上面的问题就是只禁用了health端点而已,而info端点没有禁用。正确的配置是:

#关闭Spring Boot端点检查
management.endpoints.enabled-by-default=false

该配置用于禁用所有Actuator端点的默认启用状态。在Spring Boot 2.x中,默认情况下大多数内置端点(如health、info、metrics等)是启用的,此配置会全局关闭所有端点的默认启用行为。

其次是检查是否有多处配置文件,或者配置重复,导致优先级高的配置覆盖了优先级低的配置,而你一直在改优先级低的配置,导致一直在配置无效。


网站公告

今日签到

点亮在社区的每一天
去签到