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

Woj*_*cki 5 java spring spring-boot spring-boot-actuator

在Spring Boot 1.x中,可以通过编程方式解决所有执行器端点。我有一个可显示所有执行器端点路径的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,在弹簧启动执行器2.0.5.RELEASE中没有此类MvcEndpoints。在春季新版本中,该类是否有替代品?

And*_*own 4

你需要的一切都在org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints豆子里。如果你能原谅这个双关语的话,这应该会让你走上正确的道路:

@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));
  }
}
Run Code Online (Sandbox Code Playgroud)

org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest当您需要从代码中执行此操作时,可帮助您为执行器端点设置 Spring 安全规则。例如,在您的WebSecurityConfigurerAdapter实现中,可以将此片段合并到您现有的规则中:

http.authorizeRequests()
      .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
      .hasAnyAuthority("ROLE_ADMIN", "ROLE_SUPPORT")
Run Code Online (Sandbox Code Playgroud)