目录

Spring Boot Actuator

Endpoints 是 Actuator 的核心部分,它用来监视应用程序及交互。

spring-boot-starter-actuator 内置了非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们扩展自己的端点。

1
2
3
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
management.endpoints.web.exposure.include=*.
1
2
3
4
5
6
7
8
management:
  endpoints:
    web:
      exposure:
        exclude: env
        include: '*'
  server:
    port: 8801

http://localhost:8080/actuator

当与Web不同端口 Filter 如何配置

  1. Add a class that’s annotated with @ManagementContextConfiguration
  2. Put that configuration file outside the component scan (so spring boot’s normal auto-config won’t find it)
  3. Declare it in META-INF/spring.factories:
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=com.ynthm.demo.config.EndpointFilterConfig

引入 Spring Security

所有 endpoints 启用 Basic 授权

1
2
3
4
5
6
spring:
  security:
    user:
      name: monitor
      password: monitor123
      roles: ENDPOINT_ADMIN
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
        http.httpBasic();
        return http.build();
    }

}

所有 endpoints 放行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().permitAll());
        return http.build();
    }

}