如何配置Spring Boot Actuator的基本URL,使其由基本端点返回?

时间:2018-10-13 01:19:30

标签: spring-boot-actuator

Spring Boot的执行器基本端点返回json,其中包含所有已启用端点的href链接。链接全部默认为localhost:8080,但是我已通过诸如machine.mydomain.com之类的主机名进行了身份验证。

如何获取要用https://machine.mydomain.com而不是http://localhost:8090填充的链接?

我访问执行器时的示例输出 https://machine.mydomain.com/actuator

/ 20181012150557
// https://machine.mydomain.com/actuator

{
  "_links": {
    "self": {
      "href": "http://localhost:8090/actuator",
      "templated": false
    },
    "beans": {
      "href": "http://localhost:8090/actuator/beans",
      "templated": false
    },
    "health": {
      "href": "http://localhost:8090/actuator/health",
      "templated": false
    }
}

我想要的是     / 20181012150557     // https://machine.mydomain.com/actuator

{
  "_links": {
    "self": {
      "href": "https://machine.mydomain.com/actuator",
      "templated": false
    },
    "beans": {
      "href": "https://machine.mydomain.com/actuator/beans",
      "templated": false
    },
    "health": {
      "href": "https://machine.mydomain.com/actuator/health",
      "templated": false
    }
}

1 个答案:

答案 0 :(得分:4)

我的应用程序遇到了同样的问题。 Spring Boot使用当前请求URL在执行器主页上生成超媒体链接。

请参见WebMvcEndpointHandlerMapping

@Override
@ResponseBody
protected Map<String, Map<String, Link>> links(HttpServletRequest request,
        HttpServletResponse response) {
    return Collections.singletonMap("_links",
            this.linksResolver.resolveLinks(request.getRequestURL().toString()));
}

如果您位于代理服务器(Apache,nginx)的后面,则实际请求URL可能会移动到X-Forwarded-*标头中。最简单的解决方案是为执行器端点添加Spring的ForwardedHeaderFilter

@Bean
public FilterRegistrationBean forwardedHeaderFilter() {
    final FilterRegistrationBean<ForwardedHeaderFilter> filter = new FilterRegistrationBean<>(new ForwardedHeaderFilter());
    filter.setName("Forwarded Header filter");
    filter.setUrlPatterns(Collections.singletonList("/actuator/*"));
    return filter;
}