如何在代理后面配置Spring HATEOAS?

Jan*_*cki 9 rest spring hateoas spring-data-rest spring-hateoas

我有Hateoas的Spring Data Rest作为我的支持.它是代理人的背后.

后端网址: backend.com

代理网址: proxy.com

当我查询代理网址时,例如http://proxy.com/items/1,我得到一个href带域名链接的回复backend.com.我需要域名proxy.com.

Mar*_*NCE 20

从Spring-Boot 2.1/Spring 5.1开始,Spring将处理X-Forwarded-*的责任从Spring HATEOAS转移到Spring MVC.

https://jira.spring.io/browse/SPR-16668

您现在需要注册过滤器bean.

最小的实施:

@Bean
FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter()
{
    FilterRegistrationBean<ForwardedHeaderFilter> bean = new FilterRegistrationBean<>();
    bean.setFilter(new ForwardedHeaderFilter());
    return bean;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用Spring HATEOAS和Spring Boot,似乎有一个参数:https://github.com/spring-projects/spring-hateoas/issues/753#issuecomment-463077217:如果代理添加了常规的X-Forwarded -For和X-Forwarded-Proto标头(大多数代理服务器都这样做),如果在application.properties中将server.use-forward-header设置为true,则应该正确呈现绝对链接。 (2认同)

Jan*_*cki 10

确保您的代理正在向传递给X-Forwarded-Host: proxy.com请求添加标头backend.com.然后Spring Hateoas将自动生成链接hrefs proxy.com.

X-Forwarded-Host 可以包含端口.

另请参阅其他支持的X-Forwarded-*标头.


yej*_*lue 5

受 Cyril Gambis 评论的启发,Spring 提供了一个属性server.use-forward-headers,该属性至少从 1.3.0.RELEASE 开始存在。从 Spring Boot 2.2.0.RELEASE 开始,该属性已被弃用,请server.forward-headers-strategy改用。

当您使用 Spring Data Rest 时,我建议 set server.forward-headers-strategy = framework,然后 Spring Hatoaes 在x-forwarded-*标头的帮助下为 href 生成代理 URI 。

参考