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
In Spring Data REST, the app wants to generate links using https://myapp.mycompany.com/foo
. How can I customize the app to force it to produce API gateway links for all rel
attributes?
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. I checked the docs section on customizing the rel
but it doesn't appear to cover this use case.
What should I do?
CodePudding user response:
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.
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
; 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. If you're using Spring MVC without Boot, add this filter to your Servlet configuration.