【问题标题】:How to configure "health" Actuator endpoint to work with token authentication?如何配置“健康”执行器端点以使用令牌身份验证?
【发布时间】:2019-07-22 02:53:52
【问题描述】:

我们在 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": { ... }
}

您是否遇到过类似的问题?你是怎么处理的?

【问题讨论】:

  • 当您访问此端点时,无论是否经过身份验证,应用程序的行为如何?
  • 现在,当设置了参数management.endpoint.health.show-details=when_authorized 并且我正在为此端点打开身份验证时,我会看到详细信息。但未经身份验证我无法进入。当我关闭端点的身份验证并设置when_authorized 时,我看不到详细信息。所以参数工作正常。但我无法弄清楚如何将/actuator/health 端点设置为在有和没有身份验证的情况下都可用......
  • @PiotrPradzynski 你是怎么解决这个问题的?我也卡在了同一个地方!

标签: spring spring-boot spring-security spring-boot-actuator


【解决方案1】:

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

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 并且您已关闭身份验证,因此主体为空。不确定我是否正确,但我希望我能给你一个线索。 :)

【讨论】:

  • 谢谢。它并没有解决我的问题,但帮助我意识到我在我的想法中混合了两件事:身份验证和授权:)
猜你喜欢
  • 2019-09-24
  • 2016-06-09
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 2018-03-12
  • 2021-04-13
  • 2021-01-03
相关资源
最近更新 更多