有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java spring启动执行器健康指示器在授权+安全时显示详细信息

我将HealthIndicator应用于spring boot应用程序(actuator 2.5.1),并尝试稍微调整一下设置。不幸的是,文档并没有“清楚”地说明默认值。当我挖的时候。。。我经历了不同的行为。 你能告诉我这是否是误解/配置错误/错误吗?多谢各位

问题:

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

不幸的是,spring安全配置使情况变得复杂。。。 我有两个望远镜

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

案例1:默认配置

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

默认配置docs 经营终点。健康显示详细信息=从不

预期响应/执行器/健康

{"status":"DOWN"}

臭虫真正的响应包含详细信息(默认值:从不)

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

对/执行器/健康/定制的预期响应没有细节,但实际响应有细节。 在默认配置中可以访问的细节在我看来是一个BUG

案例2:配置更改

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

预期的行为是,因为我没有将安全性添加到/exactor/**。任何请求都将被视为未经授权处理,因此它应该返回json而不提供详细信息。 对/actuator/health的实际响应与预期一样-只是状态:{"status":"DOWN"}。但是/actuator/health/custom的真正响应是没有找到HTTP404吗?!?为什么禁用整个自定义运行状况终结点?如果有的话,我希望HTTP 401未经验证看起来像一只虫子无论如何,我希望/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。我不知道如何正确设置文档中提到的安全性:

If you have secured your application and wish to use always, your security configuration must permit access to the health endpoint for both authenticated and unauthenticated users.


共 (2) 个答案

  1. # 1 楼答案

    仅返回此项并检查状态:

    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
    
  2. # 2 楼答案

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

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