【问题标题】:Enable CORS for /health endpoint in Spring Boot Actuator在 Spring Boot Actuator 中为 /health 端点启用 CORS
【发布时间】: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");
        }
    };
}

【问题讨论】:

    标签: spring spring-boot cors spring-boot-actuator


    【解决方案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 - 你可以试试这个方法:stackoverflow.com/questions/42874351/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2020-09-15
      • 2022-07-08
      相关资源
      最近更新 更多