spring - 如何配置 "health"Actuator 端点以使用 token 身份验证?

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

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

我找到了属性management.endpoint.health.show-details=when_authorized,它应该会有帮助,但现在我正在与 Spring Security 配置进行斗争,以允许每个人看到:

{
  "status": "UP"
}

/actuator/health 下,而使用 token 授权的用户应该看到:

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

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

最佳答案

好的,现在我明白了,如果您关闭应用程序中的安全性并保留 management.endpoint.health.show-details=when_authorized 您将仅获得 status 字段?如果我是对的,这不是问题,请查看 Spring 类 HealthWebEndpointResponseMapper 中的 map 方法。正如我发现的,如果 if 中的条件为 true,此方法会覆盖(从响应中删除 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 并且您已关闭身份验证,因此主体为空。不确定我是否正确,但我希望我能给你一个线索。 :)

关于spring - 如何配置 "health"Actuator 端点以使用 token 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54921495/

相关文章:

spring-security-oauth2 2.0.7 刷新 token UserDetailsS​​ervice 配置 - UserDetailsS​​ervice 是必需的

java - Spring 3 MVC从 Controller 访问全局变量

Spring Rest Template 发送JsonArray

spring - 在特定 URL 中实时加载 React App

java - akka-spring : How to wire actors

java - 了解 Spring 上下文初始化顺序

java - 编写Spring规范来过滤不相关的表

maven - 从 Gradle 构建内部复制 Maven "dependencyManagement"标签

spring - 将 Spring Boot 应用程序部署为 Windows 服务

java - 带有 Spring Security 的 AspectJ