1. 概述
在本教程中,我們將學習如何啓用 Spring Boot Actuator 中的所有端點。我們首先將處理必要的 Maven 依賴項。然後,我們將通過檢查我們的屬性文件來控制我們的端點。最後,我們將對如何安全我們的端點進行概述。
Spring Boot 1.x 和 Spring Boot 2.x 之間在配置 Actuator 端點方面存在一些變化。我們將注意這些變化出現時。
2. 安裝配置
為了使用 Actuator,我們需要將其包含在我們的 Maven 配置中:spring-boot-starter-actuator。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>3.1.2</version>
</dependency>此外,從 Spring Boot 2.0 開始,如果希望通過 HTTP 暴露我們的端點,則需要包含 web starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
</dependency>3. 啓用和暴露端點
從 Spring Boot 2 開始,我們需要啓用並暴露我們的端點。 默認情況下,所有端點(除了 /shutdown)都已啓用,並且只有 /health 和 /info 可暴露。 所有端點都位於 /actuator 下,即使我們為應用程序配置了不同的根上下文。
這意味着,一旦我們在 Maven 配置中添加了適當的啓動器,我們就可以通過以下方式訪問 /health 和 /info 端點:http://localhost:8080/actuator/health 和 http://localhost:8080/actuator/info。
讓我們訪問 http://localhost:8080/actuator 並查看可用的端點列表,因為 actuator 端點啓用了 HATEOS。 您應該會看到 /health 和 /info。
{"_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}}}3.1. 公開所有端點
現在,通過修改我們的 application.properties文件,公開所有端點,但不包括 /shutdown 路徑:
management.endpoints.web.exposure.include=*我們重啓服務器並再次訪問 actuator</em> 端點後,應能看到除了 shutdown</em> 端點之外的其他端點可用。
{"_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}}}3.2. 暴露特定端點
某些端點可能會暴露敏感數據,因此我們需要更精細地控制我們暴露的端點。
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=threaddumpinclude 和 exclude 屬性都接受一個端點的列表。exclude 屬性優先於 include。
3.3. 啓用特定端點
接下來,讓我們學習如何更精細地控制已啓用的端點。
首先,我們需要關閉默認設置,該設置啓用了所有端點:
management.endpoints.enabled-by-default=false接下來,讓我們啓用並暴露僅 /health 端點:
management.endpoint.health.enabled=true
management.endpoints.web.exposure.include=health使用此配置,我們只能訪問 /health 端點。
3.4. 啓用關閉功能
由於其敏感性,/shutdown 端點默認情況下已禁用。
現在,通過在我們的 application.properties 文件中添加以下行來啓用它:
management.endpoint.shutdown.enabled=true現在當我們查詢 /actuator 端點時,應該看到它被列出。 /shutdown 端點僅接受 POST 請求,所以讓我們優雅地關閉我們的應用程序:
curl -X POST http://localhost:8080/actuator/shutdown4. 端點安全保障
在實際應用中,我們通常需要在應用程序中實現安全機制。考慮到這一點,讓我們為我們的 actuator 端點添加安全保護。
首先,通過添加 Spring Boot Starter Security Maven 依賴項,來增強應用程序的安全性:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.1</version>
</dependency>為了最基本的安全性,我們只需要這樣做。 僅僅通過添加 Security Starter,我們已經自動地為所有暴露的端點應用了基本的身份驗證,除了 /info 和 /health。
現在,讓我們自定義安全設置,限制 /actuator 端點到 ADMIN 角色。
讓我們從排除默認的安全配置開始:
@SpringBootApplication(exclude = {
SecurityAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class
})讓我們記錄 ManagementWebSecurityAutoConfiguration.class,因為這能讓我們將自己的安全配置應用到 /actuator。
在我們的配置類中,讓我們配置幾個用户和角色,以便我們有一個 ADMIN 角色進行使用:
@Bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
UserDetails admin = User.withDefaultPasswordEncoder()
.username("admin")
.password("password")
.roles("USER", "ADMIN")
.build();
return new InMemoryUserDetailsManager(user, admin);
}Spring Boot 提供了便捷的請求匹配器,用於我們的 actuator 端點。
我們使用它來限制對 /actuator 的訪問,僅限 ADMIN 角色。
http.authorizeHttpRequests(authz -> {
authz.requestMatchers(mvc.pattern("/actuator/**"))
.hasRole("ADMIN")
.anyRequest()
.authenticated();
});5. 結論
在本教程中,我們學習了 Spring Boot 如何默認配置 actuator。之後,我們自定義了在 application.properties 文件中啓用的、禁用的和暴露的端點。由於 Spring Boot 默認以不同的方式配置 /shutdown 端點,因此我們學習瞭如何單獨啓用它。
之後,我們學習瞭如何配置 actuator 安全性。