简体   繁体   English

如何在 spring-data-rest 链接中重写应用程序的基础 URL?

[英]How do I rewrite the app's base URL in spring-data-rest links?

My company uses an API gateway that transforms URLs as follows, modifying both the hostname and the base path of the resource: https://apigateway.mycompany.com/myapp/foo -> https://myapp.mycompany.com/foo我公司使用 API 网关,按如下方式转换 URL,修改主机名和资源的基本路径: https://apigateway.mycompany.com/myapp/foo -> https://myapp.mycompany.com/foo

In Spring Data REST, the app wants to generate links using https://myapp.mycompany.com/foo .在 Spring 数据 REST 中,应用程序想要使用https://myapp.mycompany.com/foo生成链接。 How can I customize the app to force it to produce API gateway links for all rel attributes?我如何自定义应用程序以强制它为所有rel属性生成 API 网关链接?

I want to change the way that the links are rendered without changing the actual paths to those resources on the myapp.mycompany.com server.我想更改链接的呈现方式,而不更改 myapp.mycompany.com 服务器上这些资源的实际路径。 I checked the docs section on customizing the rel but it doesn't appear to cover this use case.我查看了有关自定义rel的文档部分,但它似乎并未涵盖此用例。

What should I do?我应该怎么办?

If Spring MVC knows about your load balancer/proxy, it can automatically rewrite generated URLs in Spring Data REST and your own controllers (with MvcUriComponentsBuilder ) to reflect the original request URL, including scheme, port, and more.如果 Spring MVC 知道您的负载均衡器/代理,它可以自动重写 Spring 数据 REST 和您自己的控制器(使用MvcUriComponentsBuilder )中生成的 URL 以反映原始请求 URL,包括方案、端口等。

There's a built-in Servlet filter named ForwardedHeaderFilter that handles this for you by inspecting well-known forwarding headers such as X-Forwarded-For ;有一个名为ForwardedHeaderFilter的内置 Servlet 过滤器,它通过检查众所周知的转发标头(例如X-Forwarded-For )来为您处理此问题; if you're using Spring Boot, you can set server.forward-headers-strategy = framework to get one registered, or you can use @Import(ForwardedHeaderFilter.class) to create a bean that Boot will then apply to the container.如果您使用的是 Spring Boot,您可以设置server.forward-headers-strategy = framework来注册一个,或者您可以使用@Import(ForwardedHeaderFilter.class)创建一个 bean,然后 Boot 会将其应用于容器。 If you're using Spring MVC without Boot, add this filter to your Servlet configuration.如果您在没有引导的情况下使用 Spring MVC,请将此过滤器添加到您的 Servlet 配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM