【问题标题】:spring boot actuator health indicator show details when authorized + securityspring boot 执行器健康指示器在授权 + 安全时显示详细信息
【发布时间】:2021-11-06 21:26:42
【问题描述】:

我将 HealthIndicator 实现到 Spring Boot 应用程序(执行器 2.5.1)并尝试稍微调整设置。不幸的是,文档并没有“清楚地”说明默认值。当我挖掘它们时……我会遇到不同的行为。 您能否指出这是否是误解/错误配置/错误?谢谢。

问题:

如何设置执行器以允许匿名调用两个端点:/actuator/health/actuator/health/custom,响应简单(无详细信息或组件)。并且两个端点都返回已验证(和授权)请求的完整详细信息。

不幸的是,spring 安全配置使情况变得复杂...... 我有两个范围

  • /api/** 用于带有 JWT 身份验证的 SPA 应用程序
  • /api2/** 用于其他具有基本身份验证的工具
  • (没有明确处理其他任何内容。因此,/actuator/** 可供所有人访问)

案例一:默认配置

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        ...
        return Health
            .status(...)
            .withDetail("detailDto", detailDto)
            .build();
    }
}

默认配置docs management.endpoint.health.show-details=从不

/actuator/health 的预期响应

{"status":"DOWN"}

BUG?:真正的响应是 WITH 详细信息(同样默认值:从不)

{
  "status": "DOWN",
  "components": {
    "blobStorage": {
      "status": "DOWN",
      "details": ...
    },
    "diskSpace": {
      "status": "UP",
      "details": ...
    },
    "custom": {
      "status": "UP",
      "details": ...
    },
    "ping": {
      "status": "UP"
    }
  }
}

/actuator/health/custom 的预期响应是没有详细信息,但实际响应是有详细信息。 在默认配置中可以访问这些细节在我看来是一个BUG

案例 2:更改配置

management.endpoint.health.show-details=when-authorized

预期的行为是,因为我没有向 /actuator/** 添加安全性。任何请求都将被视为未经授权,因此它应该返回没有详细信息的 json。 /actuator/health 的真实响应与预期一致 - 只是状态:{"status":"DOWN"}。但是/actuator/health/custom 的真正响应是 HTTP 404 NOT FOUND ?!?为什么禁用整个自定义健康端点?如果有任何比我预期的 HTTP 401 未经身份验证。 看起来像一个 BUG。 无论如何,我预计 /actuator/health/custom 会起作用,但没有详细信息: {"status":"DOWN"}

案例 3:更改配置

management.endpoint.health.show-components=when-authorized
management.endpoint.health.show-details=when-authorized

并为/actuator/**添加安全性

    @Configuration
    @Order(3)
    public static class ActuatorSecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
           ...
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/actuator/**")
                    .authorizeRequests(authorize -> authorize
                            .anyRequest().permitAll()
                    )
                    .httpBasic();
                    // ?????
        }
    }

端点/actuator/health 返回{"status":"DOWN"} 以获取预期的已验证请求的匿名和完整详细信息。但是端点/actuator/health/custom 仍然只适用于经过身份验证的请求。匿名调用返回 404 NOT FOUND。我不知道如何正确设置文档中提到的安全性:

如果您已保护应用程序并希望始终使用,则您的安全配置必须允许经过身份验证和未经身份验证的用户访问健康端点。

【问题讨论】:

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


    【解决方案1】:

    授予对所有执行器端点的访问权限:

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatcher("/actuator/**")
                    .permitAll()
                    .anyRequest().authenticated()
                    .and()
                    ...
        }
    

    【讨论】:

    • 不幸的是,不起作用(甚至无法编译)。
    【解决方案2】:

    只返回这个并检查状态:

    builder.build().getStatus()  OR
    builder.build().getStatus().getCode()
    

    我通常这样做,而不是获取所有细节。

    还保留这样的属性:

    management:
      endpoint:
        health:
          sensitive: false
          show-details: never
        dependency-health-check:
          enabled: false
        diskspace:
          enabled: false
        details:
          enabled: false
        application:
          enabled: true
    

    【讨论】:

    • 它适用于弹簧启动执行器 1.* ...我使用的是 v2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2018-03-28
    相关资源
    最近更新 更多