简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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