概述

本文将介绍如何启用Spring Boot Actuator的所有Endpoints。首先从maven依赖开始,然后讲解如何通过配置文件来控制Endpoint(后称作端点)。最后再学习一下如何确保端点的安全。

其中Spring Boot 1.x和Spring Boot 2.x在Actuator的端点配置上会有一定的区别。当出现区别时,会进行提示。

引入依赖

要使用Spring Boot Actuator需要先在项目中引入对应的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.4.3</version>
</dependency>

从Spring Boot 2.x开始,如果想通过HTTP的方式进行访问,还需要引入web starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.3</version>
</dependency>

启用并暴露端点

从Spring Boot 2.x开始,我们需要手动的去启用和暴露端点。默认情况下,除了/shutdown之外的所有端点都是可用的,同时只有/health和/info端点是对外暴露的。即便我们为应用程序配置了多个根上下文,所有的端点都可以通过/actuator来进行查找。

这意味着,一旦我们引入了合适的starter到maven配置中,我们便可以通过http://localhost:8080/actuator/health和http://localhost:8080/actuator/info来进行两个端点的访问。

访问http://localhost:8080/actuator,返回可用的端点列表如下:

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

暴露所有端点

下面通过配置来暴露除了/shutdown之外的所有端点,在application.properties中进行如下配置:

management.endpoints.web.exposure.include=*

重启,再次访问/actuator,可以看到除了/shutdown之外的其他所有端点。

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},
"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false},
"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},
"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},
"env":{"href":"http://localhost:8080/actuator/env","templated":false},
"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},
"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},
"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},
"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},
"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},
"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}

暴露指定端点

一些端点可能会暴露敏感数据,所以我们需要知道如何细粒度去暴露端点。

management.endpoints.web.exposure.include属性可以通过逗号分隔符来配置多个端点,比如我们暴露/beans和/loggers端点:

management.endpoints.web.exposure.include=beans, loggers

除了包含具体端点之外,我们还可以排除一些端点。比如暴露除了/threaddump之外的所有端点:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=threaddump

include和exclude属性均支持配置一个端点列表。exclude属性的优先级要高于include。

开启指定端点

下面来看一下如何细粒度的开启指定的端点。首先,需要关闭默认开启的所有端点:

management.endpoints.enabled-by-default=false

开启并暴露/health端点:

management.endpoint.health.enabled=true
management.endpoints.web.exposure.include=health

通过上述配置,便可以访问/health端点了。

开启Shutdown端点

因为/shutdown端点比较敏感的原因,该端点默认是不可用的。可在application.properties文件中通过如下配置进行启动:

management.endpoint.shutdown.enabled=true

此时,访问/actuator端点,就可以在列表中看到它了。/shutdown端点仅接收POST请求,可以通过如下方式优雅的关闭服务:

curl -X POST http://localhost:8080/actuator/shutdown

端点的安全

在真实的应用中,我们通常需要对应用程序进行安全防护。基于此,我们需要确保Actuator端点的安全。

首先,在应用程序中添加security的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.4.4</version>
</dependency>

为了最基本的安全保障,这是我们需要做的事。添加security starter依赖之后,程序会自动将身份验证应用于除/info和/health之外的所有公开端点。

下面,自定义security以将 /actuator 端点限制为 ADMIN 角色。

首先排除默认的安全配置:

@SpringBootApplication(exclude = { 
    SecurityAutoConfiguration.class, 
    ManagementWebSecurityAutoConfiguration.class 
})

留意下ManagementWebSecurityAutoConfiguration.class类,它会让安全配置应用到 /actuator。

在我们的配置类中,配置几个用户和角色,同时有一个ADMIN的角色可以使用:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
    auth
      .inMemoryAuthentication()
      .withUser("user")
      .password(encoder.encode("password"))
      .roles("USER")
      .and()
      .withUser("admin")
      .password(encoder.encode("admin"))
      .roles("USER", "ADMIN");
}

SpringBoot 为我们提供了一个方便的请求匹配器,用于我们的Actuator端点。利用它可以将/actuator锁定为ADMIN角色:

http.requestMatcher(EndpointRequest.toAnyEndpoint())
  .authorizeRequests((requests) -> requests.anyRequest().hasRole("ADMIN"));

小结

通过本文,我们学习了Spring Boot是如何来默认配置Actuator的。随后,我们在应用程序的application.properties文件中定义了端点的启用、禁用和暴露。鉴于Spring Boot对/shutdwon端点的不同处理,我们学习了如何单独启用该端点。最后,学习了如何基于security对端点来进行安全防护。

源码地址:https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-actuator 。
原文链接: https://www.baeldung.com/spring-boot-actuator-enable-endpoints



如何启用Spring Boot Actuator的所有Endpoints插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://choupangxia.com/2021/07/14/spring-boot-actuator-enabled-endpoints/