简体   繁体   English

如何使用 spring boot 2 以编程方式获取所有执行器端点?

[英]How to get all actuator endpoints programatically using spring boot 2?

In spring boot 1.x it was possible to resolve all actuator endpoints programatically.在 spring boot 1.x 中,可以以编程方式解析所有执行器端点。 I have a bean that exposes all actuator endpoints paths我有一个暴露所有执行器端点路径的 bean

@Component
public class MyProjectActuatorEndpoints {

    @Autowired
    private MvcEndpoints endpoints;

    public String[] getActuatorEndpointPaths() {
        return endpoints.getEndpoints().stream()
            .map(MvcEndpoint::getPath)
            .map(path -> path + "/**")
            .toArray(String[]::new);
    }
}

Unfortunately in spring boot actuator 2.0.5.RELEASE there is no such class MvcEndpoints.不幸的是,在 spring boot 执行器 2.0.5.RELEASE 中没有这样的类 MvcEndpoints。 Is there any replacement for this class in the new spring version?这个类在新的spring版本中是否有替代品?

Spring Boot Actuator 2.x exposes /actuator endpoints as configurable environment variables. Spring Boot Actuator 2.x 将/actuator端点公开为可配置的环境变量。

Enabling Acutator Endpoints启用 Acutator 端点

You can enable these actuator endpoints in your application.properties您可以在 application.properties 中启用这些执行器端点

management.endpoints.web.exposure.include=info, health

or (with extreme caution) enable them all.或(特别小心)全部启用它们。 Keep in mind that many of these are sensitive in nature.请记住,其中许多本质上是敏感的。

management.endpoints.web.exposure.include=*

Securing Actuator Endpoints (reference)保护执行器端点(参考)

The documentation specifies this as a strategy to secure all endpoints.文档将此指定为保护所有端点的策略。 The EndpointRequest itself would be the closest alternative to what you were looking for ( MvcEndpoints ) EndpointRequest本身将是您正在寻找的最接近的替代品( MvcEndpoints

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
            .anyRequest().hasRole("ENDPOINT_ADMIN")
            .and()
            .httpBasic();
    }

}

You may also set up a particular antmatcher in case you have a different strategy or role that you would like to assign just to these endpoints你也可以设置一个特定的 antmatcher,以防你有一个不同的策略或角色,你只想分配给这些端点

httpRequest.authorizeRequests().antMatcher("/actuator/*").hasRole("ENDPOINT_ADMIN")
@Autowired
private HealthEndpoint healthEndpoint;

public Health getAlive() {
    return healthEndpoint.health();
}

Everything you need is in the org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints bean.您需要的一切都在org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints bean 中。 This should set you on the right path, if you'll pardon the pun:如果你原谅双关语,这应该让你走上正确的道路:

@Slf4j
@Component
public class ActuatorLogger {

  public ActuatorLogger(@Autowired PathMappedEndpoints pme) {
    log.info("Actuator base path: {}", pme.getBasePath());
    pme.getAllPaths().forEach(p -> log.info("Path: {}", p));
  }
}

org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest is available to help you set spring security rules for actuator endpoints when you need to do it from code. org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest可用于帮助您在需要从代码中执行时为执行器端点设置弹簧安全规则。 For example, in your WebSecurityConfigurerAdapter implementation, this fragment could be merged in to your existing rules:例如,在您的WebSecurityConfigurerAdapter实现中,此片段可以合并到您现有的规则中:

http.authorizeRequests()
      .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
      .hasAnyAuthority("ROLE_ADMIN", "ROLE_SUPPORT")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM