升级Spring Boot 2到3后AWS Elastic Load Balancer健康检查失败
Spring Boot 3 升级后 AWS ELB 健康检查403问题排查与解决
原因分析
- Spring Security 默认权限收紧:Spring Boot 3 调整了 Spring Security 与 Actuator 的集成规则——只要项目依赖 Spring Security,所有 Actuator 端点(包括核心的
/actuator/health健康检查端点)默认会被强制要求认证。而 Spring Boot 2 中该端点默认公开,无需凭证即可访问,这直接导致 ELB 无认证的健康检查请求被拒绝,返回403状态码。 - Actuator 暴露配置兼容问题:虽然 Spring Boot 3 默认暴露
health端点,但如果你的配置沿用了 Spring Boot 2 的旧规则(比如未明确声明management.endpoints.web.exposure.include),可能存在端点未正确对外暴露的情况(此场景概率较低,但需排查)。
解决建议
方案1:放行健康检查端点(推荐)
在 Spring Security 配置中明确允许/actuator/health路径无需认证即可访问,同时确保 Actuator 端点正确暴露:
- 配置 Spring Security 过滤器链:
@Configuration public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .requestMatchers("/actuator/health").permitAll() .anyRequest().authenticated() ); return http.build(); } }
- 在
application.properties(或application.yml)中确认 Actuator 配置:
# 确保健康端点被暴露 management.endpoints.web.exposure.include=health # 可选:若ELB需要查看健康详情,开启该配置 management.endpoint.health.show-details=always
方案2:给ELB健康检查添加认证凭证
如果业务要求必须保护健康检查端点,可修改 AWS 目标组的健康检查配置,让请求携带认证信息:
- 进入 AWS 控制台的目标组
ocorp-dev-tg配置页面 - 在「健康检查」模块,添加自定义HTTP头,格式为
Authorization: Basic <Base64编码的用户名:密码> - 确保 Spring Security 中已配置对应权限的账号
方案3:临时回退权限行为(不推荐)
若需快速恢复 Spring Boot 2 的默认行为,可在配置文件中关闭 Actuator 的安全保护,但该配置已被标记为废弃,仅建议作为临时过渡:
management.security.enabled=false
内容的提问来源于stack exchange,提问作者okan
相关产品推荐
相关产品推荐

