Current implementation of ControllerLinkBuilder relies on Spring's ServletUriComponentsBuilder to construct links. The problem with ServletUriComponentsBuilder is that it is poorly designed (uses only static method calls), eliminating the possibility to extend and enhance the behaviour.
One typical scenario that I discovered yesterday:
Our tomcat server is hosted behind Apache. The client (browser) accesses a URL using www.example.com/path/to/resource. However, because of the way Apache has been set up, it doesn't send along the Host header value of www.example.com to Tomcat. Instead, it sends the internal IP address. The original hostname of www.example.com is set in an X-Forwarded-Host header.
Now, because Spring's ServletUriComponentsBuilder uses request.getServerName() to construct the hostname part of the URL, any generated URLs will have the internal IP address instead of www.example.com.
The only way I can fix this currently is by either:
- Creating a
HttpServletRequestWrapper that takes the value of X-Forwarded-Host header and sets it to Host header.
OR
- Rewriting
ControllerLinkBuilder logic to not use Spring's ServletUriComponentsBuilder.
Current implementation of
ControllerLinkBuilderrelies on Spring'sServletUriComponentsBuilderto construct links. The problem withServletUriComponentsBuilderis that it is poorly designed (uses only static method calls), eliminating the possibility to extend and enhance the behaviour.One typical scenario that I discovered yesterday:
Our tomcat server is hosted behind Apache. The client (browser) accesses a URL using
www.example.com/path/to/resource. However, because of the way Apache has been set up, it doesn't send along theHostheader value ofwww.example.comto Tomcat. Instead, it sends the internal IP address. The original hostname ofwww.example.comis set in anX-Forwarded-Hostheader.Now, because Spring's
ServletUriComponentsBuilderusesrequest.getServerName()to construct the hostname part of the URL, any generated URLs will have the internal IP address instead ofwww.example.com.The only way I can fix this currently is by either:
HttpServletRequestWrapperthat takes the value ofX-Forwarded-Hostheader and sets it toHostheader.OR
ControllerLinkBuilderlogic to not use Spring'sServletUriComponentsBuilder.