endpoint 是我们使用 Spring Boot Actuator
最需要关心的对象,列举一些你可能感兴趣的 endpoint
ID | Description
|
---|---|
beans | 查看 Spring 容器中的所有对象 |
configprops | 查看被 @ConfigurationProperties 修饰的对象列表 |
env | 查看 application.yaml 配置的环境配置信息 |
health | 健康检查端点 |
info | 应用信息 |
metrics | 统计信息 |
mappings | 服务契约 @RequestMapping 相关的端点 |
shutdown | 优雅下线 |
例如 health
,只需要访问如下 endpoint 即可获取应用的状态
curl "localhost:8080/actuator/health"
第三步 了解 endpoint 的 enable 和 exposure 状态Spring Boot Actuator
针对于所有 endpoint 都提供了两种状态的配置
shutdown
之外,其他 endpoint 都是启用状态。这也很好理解,其他 endpoint 基本都是查看行为,shutdown 却会影响应用的运行状态。exposure 暴露状态。endpoint 的 enabled 设置为 true 后,还需要暴露一次,才能够被访问,默认情况下只有 health 和 info 是暴露的。enabled 不启用时,相关的 endpoint 的代码完全不会被 Spring 上下文加载,所以 enabled 为 false 时,exposure 配置了也无济于事。
几个典型的配置示例如下
启用并暴露所有 endpoint
management: endpoints: web: exposure: include: "*" endpoint: shutdown: enabled: true
只启用并暴露指定 endpoint
management: endpoints: enabled-by-default: false endpoint: info: enabled: true endpoints: web: exposure: include: "info"
禁用所有 endpoint
management: endpoints: enabled-by-default: false
或者,去除掉 spring-boot-starter-actuator
依赖!
从上文的介绍可知,有一些 Spring Boot Actuator
提供的 endpoint 是会将应用重要的信息暴露出去的,以 env
为例来感受下一个典型的 application.yaml
的示例。
server: port: 8080spring: datasource: url: jdbc:mysql://testDbHost:3306/kirito username: kirito password: 123456kirito: ak: kirito@xxx_ak sk: kirito@xxx_skmanagement: endpoints: web: exposure: include: "*"
上面的配置再经典不过,我们看看访问 localhost:8080/actuator/env
之后的返回值
{ "activeProfiles": [], "propertySources": [ { "name": "server.ports", "properties": { "local.server.port": { "value": 8080 } } }, { "name": "Config resource "class path resource [application.yaml]" via location "optional:classpath:/"", "properties": { "server.port": { "value": 8080, "origin": "class path resource [application.yaml] - 2:9" }, "spring.datasource.url": { "value": "jdbc:mysql://testDbHost:3306/kirito", "origin": "class path resource [application.yaml] - 5:44" }, "spring.datasource.username": { "value": "kirito", "origin": "class path resource [application.yaml] - 6:15" }, "spring.datasource.password": { "value": "******", "origin": "class path resource [application.yaml] - 7:15" }, "kirito.ak": { "value": "kirito@xxx_ak", "origin": "class path resource [application.yaml] - 10:7" }, "kirito.sk": { "value": "kirito@xxx_sk", "origin": "class path resource [application.yaml] - 11:7" }, "management.endpoints.web.exposure.include": { "value": "*", "origin": "class path resource [application.yaml] - 17:18" } } } ]}
可以发现,对于内置的敏感配置信息 spring.datasource.password
,Spring Boot Actuator
是进行了脱敏的,但是对于自定义的一些敏感配置,如 kirito.ak 和 kirito.sk 却被暴露出来了。
可能有的读者会立马提出质疑:我们的机器都部署内网,并且一般都是通过反向代理对外暴露的服务,这类 endpoint 是不会被外部用户访问到的。那我只能说太天真了,例如以下情况都是导致安全漏洞的真实 case:
反向代理误配置了根节点,将 actuator 的 endpoint 和 web 服务一起暴露了出去线上配置没问题,测试环境部署时开通了公网 SLB,导致 actuator 的 endpoint 暴露了出去同一环境中某台机器被攻陷,导致应用配置信息泄露安全建议针对 Spring Boot Actuator
提供的 endpoint,采取以下几种措施,可以尽可能降低被安全攻击的风险
management.endpoints.web.exposure.include=*
。为 endpoint 配置独立的访问端口,从而和 web 服务的端口分离开,避免暴露 web 服务时,误将 actuator 的 endpoint 也暴露出去。例:management.port=8099
。引入 spring-boot-starter-security
依赖,为 actuator 的 endpoint 配置访问控制。慎重评估是否需要引入 spring-boot-stater-actuator
。以我个人的经验,我至今还没有遇到什么需求是一定需要引入spring-boot-stater-actuator
才能解决,如果你并不了解上文所述的安全风险,我建议你先去除掉该依赖。今天,你使用 Spring Boot Actuator 了吗?
关键词: