Skip to content

Add support for X-Forwarded-Host in ServletUriComponentsBuilder [SPR-10110] #14743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 19, 2012 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 19, 2012

Adarsh Ramamurthy opened SPR-10110 and commented

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 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 ServletUriComponentsBuilder logic.

Reference URL: spring-projects/spring-hateoas#33

Issue Links:

Referenced from: commits 153508a

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

We'll look into X-Forwarded-Host support in ServletUriComponentsBuilder. In the mean time you can do something like this:

ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequestUri();

String host = ... ;
builder.host(host);

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Modified title (was: "ServletUriComponentsBuilder should be extensible")

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Link to temporary fix in spring-hateoas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants