一、SpringBoot Actuator
SpringBoot Actuator是SpringBoot项目中的一个监控机制,用于提供了一系列对SpringBoot项目的状态监控,有助于监控项目上线后的运行状态、组件状态等。这些监控项称之为端点(endpoint),可以通过API接口进行访问,但是配置不当可能出现敏感信息泄露,导致一系列严重的后果。
二、环境搭建
1、SpringBoot:2.3.2.RELEASE
2、SpringBoot Actuator:2.4.1
3、POM.xml文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
三、服务访问及存在的问题
启用IDEA后,访问http://127.0.0.1:8080/actuator
可以看到默认有两个端点信息,分别是health和info
1、health:显示的应用的健康信息,访问:http://127.0.0.1:8080/actuator/health,默认情况下只显示简要的状态信息:
2、info:显示的是应用配置的info信息,访问:http://127.0.0.1:8080/actuator/info,未配置info信息时,显示如下:
现在在application.yml中配置如下信息:
重启应用后再次访问http://127.0.0.1/actuator/info,可以看到配置文件中的信息被泄露出来了:
四、暴露更多信息
(一)更多的Actuator端口信息可以参考:https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#production-ready-endpoints。端口可以通过启用以及暴露两个步骤完成。
1.启用Actuator端点
默认情况下,除shutdown端点外都是启用的,启动命令:
management.endpoint.shutdown.enabled=true
2.暴露端点
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
(二)在项目中暴露所有的节点,显示env信息,展示health详细信息
(三)访问:http://127.0.0.1:8080/actuator,可以看到显示了更多的信息:
五、修复与控制
可以看以如果配置不当,可能造成严重的信息泄露,因此当项目上线时一定要进行控制,可以采用多种方式进行:
(一)采用Springsecurity安全框架对/actuator的请求进行拦截;
(二)按照最少的原则开启需要的端点;
(三)在应用前端通过Nginx等中间件对/actuator进行过滤。