0

I would like to use Spring Data REST with HATEOAS links behind an API Gateway. My native API in Spring Data REST is configured with spring.data.rest.basePath=/nativeapi, so the resources will be available at, e.g., http://localhost:8090/nativeapi/people.

In my API Gateway, I would like to redirect requests for /proxyapi to /nativeapi on the native API. For example, requests for http://localhost:8080/proxyapi/people will be redirected to http://localhost:8090/nativeapi/people.

Now I would like to have the HATEOAS links in the response be correctly pointing at the API Gateway URL with the right prefix. How can I do that in Spring Data REST?

I know that I can add a ForwardedHeaderFilter which will inspect the x-forwarded headers, but, as far as I know, this will only let me change the protocol, the hostname, the port, and add a prefix. But I have to replace the prefix /nativeapi in the HATEOAS links by /proxyapi (in addition to changing the hostname, port, and maybe also the protocol). How can I do that? Thanks!

Just an idea: The API Gateway could send a custom HTTP request header like x-forwarded-replace-prefix=/nativeapi that will tell the Spring Data REST application to replace the /nativeapi prefix in its HATEOAS links by the prefix it gets in the x-forwarded-prefix=/proxyapi header. Would that be possible?

  • Look at my answer to question [How to configure Spring HATEOAS behind proxy?](https://stackoverflow.com/questions/30020188/how-to-configure-spring-hateoas-behind-proxy/61838487#61838487). – yejianfengblue May 01 '21 at 12:07
  • Thanks for the comment @yejianfengblue. Unfortunately, this does not help in my case. It will only let me change the protocol, the hostname, the port, and add a prefix. But I have to replace the prefix /nativeapi in the HATEOAS links by /proxyapi (in addition to changing the hostname, port, and maybe also the protocol). Any idea? – Thomas Hesse May 01 '21 at 12:30
  • If your API gateway sets the forward headers, Spring Data REST and HATEOAS should replace the prefix. I have a [Spring Gateway](https://github.com/yejianfengblue/steins-gate-airline/tree/develop/sga-api-gateway/) running on port 8080, a [Netflix Eureka server](https://github.com/yejianfengblue/steins-gate-airline/tree/develop/sga-discovery) and an API service `sga-booking` running on port 30200 . The API gateway forwards request to `localhost:8080/sga-booking` to `localhost:30200`. The sga-booking generates HATEOAS link with href `localhost:8080/sga-booking`. – yejianfengblue May 01 '21 at 13:06
  • Sorry I misunderstand your question at the first time. Please explain your motivation. Why not set `spring.data.rest.basePath=/` and just let `http://localhost:8080/proxyapi/people` be forwarded to `http://localhost:8090/people`? – yejianfengblue May 01 '21 at 14:34
  • If you set `spring.data.rest.basePath=/nativeapi` and `server.forward-headers-strategy=framework`, then `http://localhost:8080/proxyapi/nativeapi/people` be forwarded to `http://localhost:8090/nativeapi/people`. – yejianfengblue May 01 '21 at 14:49
  • The `spring.data.rest.basePath=/nativeapi` is used by Spring Data REST while the forwarded headers and prefix conversion are handled by Spring MVC. In other words, Spring Data REST is not aware of the forwarded headers and Spring MVC is not aware of the rest base path. So simply say, your requirement can't be achieved in Spring world. Maybe you should consider replace the prefix in the gateway. – yejianfengblue May 01 '21 at 14:52

0 Answers0