首页 > 解决方案 > 如何配置“健康”执行器端点以使用令牌身份验证?

问题描述

我们在 Spring Boot 2 应用程序中使用基于令牌的身份验证(使用 Spring Security)。现在我正在向它介绍 Spring Boot Actuator。我想将/health端点配置为在没有任何权限的情况下可见,但仅在获得授权时才显示健康检查详细信息。

我找到了management.endpoint.health.show-details=when_authorized应该有用的属性,但现在我正在与 Spring Security 配置作斗争,以让每个人都能看到:

{
  "status": "UP"
}

/actuator/health使用令牌授权的用户应该看到:

{
  "status": "UP",
  "details": { ... }
}

你遇到过类似的问题吗?你是怎么处理的呢?

标签: springspring-bootspring-securityspring-boot-actuator

解决方案


好的,现在我明白了,如果您关闭应用程序中的安全性并让management.endpoint.health.show-details=when_authorized您获得公正status的领域?如果我是对的,这不是问题,请查看 spring 类中的HealthWebEndpointResponseMapper方法map。我发现details如果条件if为真,此方法将覆盖(从响应中删除字段):

public WebEndpointResponse<Health> map(Health health, SecurityContext securityContext,
        ShowDetails showDetails) {
    if (showDetails == ShowDetails.NEVER
            || (showDetails == ShowDetails.WHEN_AUTHORIZED
                    && (securityContext.getPrincipal() == null
                            || !isUserInRole(securityContext)))) {
        health = Health.status(health.getStatus()).build();
    }
    Integer status = this.statusHttpMapper.mapStatus(health.getStatus());
    return new WebEndpointResponse<>(health, status);
}

在您的情况下,我猜您已将上述属性设置为when_authorized也已关闭身份验证,因此主体为空。不确定我是否正确,但我希望我能给你一个线索。:)


推荐阅读