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

18
我们希望为由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");
        }
    };
}
2个回答

25

有一些属性可用于启用执行器端点的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端点进行操作的方法。 - Jon Erickson
3
需要翻译的内容:也需要注意,为了使其适用于所有来源,我们在 .yml 文件中设置了 endpoints.cors.allowed-origins: "*",* 周围的引号很重要。翻译后的结果:To make it applicable for all sources, we set endpoints.cors.allowed-origins: "*" in the .yml file. It is important to include the quotes around the * symbol. - Jon Erickson
@Jon Erickson - 你可以尝试这种方法:https://dev59.com/q1gQ5IYBdhLWcg3wXizH - Witold Kaczurba

7

Spring Boot 2:

您需要添加以下内容来允许OPTIONS和请求头:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接