Actuator监控数据安全防护最佳实践
在Spring Boot应用中,Actuator提供了丰富的监控和管理功能,但其默认配置存在严重的安全风险。本文将通过实际案例揭示常见安全隐患并提供防护方案。
问题复现步骤
- 创建Spring Boot项目,添加actuator依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-
默认配置下访问
http://localhost:8080/actuator,会发现所有端点信息暴露 -
更危险的是,敏感数据如数据库密码、JWT密钥等可能通过
/env或/configprops端点泄露
安全防护方案
方案一:禁用敏感端点
management:
endpoints:
enabled-by-default: false
endpoint:
health:
enabled: true
info:
enabled: true
方案二:配置访问权限
management:
endpoint:
health:
roles: ADMIN
方案三:使用自定义安全配置
@Configuration
public class ActuatorSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeHttpRequests(authz -> authz
.requestMatchers(EndpointRequest.to(HealthEndpoint.class))
.permitAll()
.anyRequest().hasRole("ADMIN")
);
}
}
建议在生产环境强制实施以上防护措施,避免因配置不当导致敏感信息泄露。

讨论