在 Spring Boot Actuator 中为 /health 端点启用 CORS

Posted

技术标签:

【中文标题】在 Spring Boot Actuator 中为 /health 端点启用 CORS【英文标题】:Enable CORS for /health endpoint in Spring Boot Actuator 【发布时间】:2016-09-01 19:53:12 【问题描述】:

我们希望为 spring boot actuator 提供的对 /health 端点的所有 GET 请求启用 Cors。

我们尝试添加以下 bean,但没有成功

@Bean
public WebMvcConfigurer corsConfigurer() 
    return new WebMvcConfigurerAdapter() 
        @Override
        public void addCorsMappings(CorsRegistry registry) 
            registry.addMapping("/health").allowedMethods("GET");
        
    ;

【问题讨论】:

【参考方案1】:

春季启动 2:

您需要添加allow OPTIONS和allow headers,如下所示

management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=OPTIONS, GET, POST
management.endpoints.web.cors.allowed-headers=*

【讨论】:

【参考方案2】:

有一个few properties 可用于为执行器的端点启用 CORS。例如,在 Spring Boot 1.x 中允许来自 example.comGET 请求:

endpoints.cors.allowed-origins=http://example.com
endpoints.cors.allowed-methods=GET

对于 Spring Boot 2:

management.endpoints.web.cors.allowed-origins=http://example.com
management.endpoints.web.cors.allowed-methods=GET

【讨论】:

这就是我们最终要做的事情,它适用于在我们的案例中工作的所有执行器端点。尽管如此,仍然没有找到一种方法来为 /health 端点做这件事。 还要注意,为了使其适用于我们在endpoints.cors.allowed-origins: "*" 文件中设置的endpoints.cors.allowed-origins: "*" 的所有组织, * 周围的引号很重要 @Jon Erickson - 你可以试试这个方法:***.com/questions/42874351/…

以上是关于在 Spring Boot Actuator 中为 /health 端点启用 CORS的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 监控 Actuator

spring boot 监控与管理(actuator)

Spring Boot整合actuator实现监控管理

使用Actuator 实现Spring Boot应用监控

Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置

Spring Boot Actuator 的“httptrace”端点在 Spring Boot 2.2.0 中不再存在