我的弹簧数据随着我的支持而随之而来。它是代理的后面。
后端URL:backend.com
代理URL:proxy.com
当我查询代理URL时,例如http://proxy.com/items/1
,我通过域backend.com
获得了href
链接的响应。我需要域是proxy.com
。
从Spring-Boot 2.1/Spring 5.1开始,Spring转移了处理X-Forwarded-*从春季Hateoas转移到Spring MVC的责任。
https://jira.spring.io/browse/spr-16668
您现在需要过滤豆的注册。
最小实现:
@Bean
FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter()
{
FilterRegistrationBean<ForwardedHeaderFilter> bean = new FilterRegistrationBean<>();
bean.setFilter(new ForwardedHeaderFilter());
return bean;
}
确保您的代理将X-Forwarded-Host: proxy.com
标题添加到请求中,该标题已传递给backend.com
。然后,春季Hateoas将自动使用proxy.com
生成链接HREF。
X-Forwarded-Host
可以包含端口。
还请参见其他受支持的X-Forwarded-*标头。
灵感来自Cyril Gambis的评论,Spring提供了属性server.use-forward-headers
,该属性至少来自1.3.0.Release。从Spring Boot 2.2.0.Release,该属性被弃用,使用server.forward-headers-strategy
。
当您使用弹簧数据休息时,我建议设置server.forward-headers-strategy = framework
,然后在x-forwarded-*
标头的帮助下,Spring Hatoaes为HREF生成了代理URI。
参考
- 春季启动文档
- 春季Hateoas文档
尽管Mariano已经回答了这一点,但我想补充一点,它适用于Spring Boot。但是,如果您不使用Spring Boot,而是在J2EE容器中部署的传统Web应用程序中使用Spring 5.1.x(例如Mine),则需要在Web应用程序的Web.xml中添加过滤器。/p>
<filter>
<filter-name>forwardedHeaderFilter</filter-name>
<filter-class>org.springframework.web.filter.ForwardedHeaderFilter</filter-class>
<init-param>
<param-name>relativeRedirects</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>forwardedHeaderFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
除此之外,您还需要将Hateoas升级到0.25.1版本,其中此问题已从Hateoas方面固定。