Skip to content

Added support for X-Forwarded-Ssl header during link construction. #112

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public UriComponentsBuilder toUriComponentsBuilder() {

/**
* Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the
* request contains an {@code X-Forwarded-Host} header.
* request contains an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an
* {@code X-Forwarded-Ssl} header
*
* @return
*/
Expand All @@ -167,6 +168,12 @@ static UriComponentsBuilder getBuilder() {
HttpServletRequest request = getCurrentRequest();
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

if(StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
builder.scheme("https");
}

String header = request.getHeader("X-Forwarded-Host");

if (!StringUtils.hasText(header)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ public void usesForwardedHostAsHostIfHeaderIsSet() {
assertThat(link.getHref(), startsWith("http://somethingDifferent"));
}

@Test
public void usesForwardedSslIfHeaderIsSet() {

request.addHeader("X-Forwarded-Ssl", "on");

Link link = linkTo(PersonControllerImpl.class).withSelfRel();
assertThat(link.getHref(), startsWith("https://"));
}

@Test
public void usesForwardedSslIfHeaderIsSetOff() {

request.addHeader("X-Forwarded-Ssl", "off");

Link link = linkTo(PersonControllerImpl.class).withSelfRel();
assertThat(link.getHref(), startsWith("http://"));
}

@Test
public void usesForwardedSslAndHostIfHeaderIsSet() {

request.addHeader("X-Forwarded-Host", "somethingDifferent");
request.addHeader("X-Forwarded-Ssl", "on");

Link link = linkTo(PersonControllerImpl.class).withSelfRel();
assertThat(link.getHref(), startsWith("https://somethingDifferent"));
}

/**
* @see #26, #39
*/
Expand Down