Skip to content

Commit 6a806ed

Browse files
gregturnodrotbohm
authored andcommitted
#713 - Centralize all Forward header handling.
Spring 5.1 is centralizing all Forward header handling. This moves critical bits into one location, making it easy to completely remove once we re-baseline against this version. Also adds a test profile to ensure Spring 5.1 doesn't break anything. Original pull request: #717.
1 parent 9e59fc9 commit 6a806ed

File tree

6 files changed

+135
-19
lines changed

6 files changed

+135
-19
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ env:
77
- PROFILE=spring43-next
88
- PROFILE=spring5
99
- PROFILE=spring5-next
10+
- PROFILE=spring51-next
1011
addons:
1112
apt:
1213
packages:

pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@
125125
</repositories>
126126
</profile>
127127

128+
<profile>
129+
<id>spring51-next</id>
130+
<properties>
131+
<spring.version>5.1.0.BUILD-SNAPSHOT</spring.version>
132+
<jackson.version>2.9.2</jackson.version>
133+
</properties>
134+
<repositories>
135+
<repository>
136+
<id>spring-libs-snapshot</id>
137+
<url>http://repo.spring.io/libs-snapshot</url>
138+
</repository>
139+
</repositories>
140+
</profile>
141+
128142
<profile>
129143

130144
<!-- Profile to be run on the CI server, JARs JavaDocs -->

src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.hateoas.mvc;
1717

18-
import static org.springframework.util.StringUtils.*;
18+
import static org.springframework.hateoas.mvc.ForwardedHeader.*;
1919

2020
import lombok.RequiredArgsConstructor;
2121
import lombok.experimental.Delegate;
@@ -26,6 +26,7 @@
2626

2727
import javax.servlet.http.HttpServletRequest;
2828

29+
import org.springframework.context.ApplicationContext;
2930
import org.springframework.hateoas.Link;
3031
import org.springframework.hateoas.TemplateVariables;
3132
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
@@ -269,20 +270,30 @@ static UriComponentsBuilder getBuilder() {
269270
}
270271

271272
HttpServletRequest request = getCurrentRequest();
272-
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);
273+
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);
273274

274-
// special case handling for X-Forwarded-Ssl:
275-
// apply it, but only if X-Forwarded-Proto is unset.
275+
// Spring 5.1 can handle X-Forwarded-Ssl headers...
276+
if (isSpringAtLeast5_1()) {
277+
return builder;
278+
} else {
279+
return handleXForwardedSslHeader(request, builder);
280+
}
281+
}
282+
283+
/**
284+
* Check if the current version of Spring Framework is 5.1 or higher.
285+
*
286+
* @return
287+
*/
288+
private static boolean isSpringAtLeast5_1() {
276289

277-
String forwardedSsl = request.getHeader("X-Forwarded-Ssl");
278-
ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader(ForwardedHeader.NAME));
279-
String proto = hasText(forwarded.getProto()) ? forwarded.getProto() : request.getHeader("X-Forwarded-Proto");
290+
String versionOfSpringFramework = ApplicationContext.class.getPackage().getImplementationVersion();
280291

281-
if (!hasText(proto) && hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
282-
builder.scheme("https");
283-
}
292+
String[] parts = versionOfSpringFramework.split("\\.");
293+
int majorVersion = Integer.parseInt(parts[0]);
294+
int minorVersion = Integer.parseInt(parts[1]);
284295

285-
return builder;
296+
return (majorVersion >= 5 && minorVersion >= 1) || (majorVersion > 5);
286297
}
287298

288299
/**

src/main/java/org/springframework/hateoas/mvc/ForwardedHeader.java

+39-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,37 +15,70 @@
1515
*/
1616
package org.springframework.hateoas.mvc;
1717

18+
import static org.springframework.util.StringUtils.*;
19+
1820
import java.util.Collections;
1921
import java.util.HashMap;
2022
import java.util.Map;
2123

24+
import javax.servlet.http.HttpServletRequest;
25+
2226
import org.springframework.util.Assert;
2327
import org.springframework.util.StringUtils;
28+
import org.springframework.web.util.UriComponentsBuilder;
2429

2530
/**
2631
* Value object to partially implement the {@literal Forwarded} header defined in RFC 7239.
2732
*
2833
* @author Oliver Gierke
2934
* @see http://tools.ietf.org/html/rfc7239
35+
* @deprecated In Spring 5.1, all Forwarded headers will by handled by Spring MVC.
3036
*/
37+
@Deprecated
3138
class ForwardedHeader {
3239

33-
public static String NAME = "Forwarded";
3440
private static final ForwardedHeader NO_HEADER = new ForwardedHeader(Collections.<String, String> emptyMap());
35-
3641
private final Map<String, String> elements;
3742

3843
private ForwardedHeader(Map<String, String> elements) {
3944
this.elements = elements;
4045
}
4146

47+
/**
48+
* Utility method to pull handling of {@literal X-Forwarded-Ssl} into a class that will be removed when rebaselined
49+
* against Spring 5.1
50+
*
51+
* @param request
52+
* @param builder
53+
* @return
54+
* @deprecated No longer needed with Spring 5.1
55+
*/
56+
@Deprecated
57+
public static UriComponentsBuilder handleXForwardedSslHeader(HttpServletRequest request,
58+
UriComponentsBuilder builder) {
59+
60+
// special case handling for X-Forwarded-Ssl:
61+
// apply it, but only if X-Forwarded-Proto is unset.
62+
63+
String forwardedSsl = request.getHeader("X-Forwarded-Ssl");
64+
ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader("Forwarded"));
65+
String proto = hasText(forwarded.getProto()) ? forwarded.getProto() : request.getHeader("X-Forwarded-Proto");
66+
67+
if (!hasText(proto) && hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
68+
builder.scheme("https");
69+
}
70+
71+
return builder;
72+
73+
}
74+
4275
/**
4376
* Creates a new {@link ForwardedHeader} from the given source.
4477
*
4578
* @param source can be {@literal null}.
4679
* @return
4780
*/
48-
public static ForwardedHeader of(String source) {
81+
static ForwardedHeader of(String source) {
4982

5083
if (!StringUtils.hasText(source)) {
5184
return NO_HEADER;
@@ -75,7 +108,7 @@ public static ForwardedHeader of(String source) {
75108
*
76109
* @return
77110
*/
78-
public String getProto() {
111+
String getProto() {
79112
return elements.get("proto");
80113
}
81114

@@ -84,7 +117,7 @@ public String getProto() {
84117
*
85118
* @return
86119
*/
87-
public String getHost() {
120+
String getHost() {
88121
return elements.get("host");
89122
}
90123
}

src/test/java/org/springframework/hateoas/TestUtils.java

+30
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import java.io.IOException;
22+
23+
import javax.servlet.ServletException;
24+
import javax.servlet.http.HttpServletRequest;
25+
2126
import org.junit.Before;
27+
import org.springframework.mock.web.MockFilterChain;
2228
import org.springframework.mock.web.MockHttpServletRequest;
29+
import org.springframework.mock.web.MockHttpServletResponse;
2330
import org.springframework.web.context.request.RequestContextHolder;
2431
import org.springframework.web.context.request.ServletRequestAttributes;
32+
import org.springframework.web.filter.ForwardedHeaderFilter;
2533

2634
/**
2735
* Utility class to ease tesing.
@@ -44,6 +52,28 @@ protected void assertPointsToMockServer(Link link) {
4452
assertThat(link.getHref(), startsWith("http://localhost"));
4553
}
4654

55+
/**
56+
* Provide a mechanism to simulate inserting a {@link ForwardedHeaderFilter} into the servlet filter chain, so
57+
* {@literal Forwarded} headers are properly inserted into the test web request.
58+
*
59+
* @see https://jira.spring.io/browse/SPR-16668
60+
*/
61+
protected void adaptRequestFromForwardedHeaders() {
62+
63+
MockFilterChain chain = new MockFilterChain();
64+
65+
try {
66+
new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
67+
} catch (ServletException e) {
68+
throw new RuntimeException(e);
69+
} catch (IOException e) {
70+
throw new RuntimeException(e);
71+
}
72+
73+
HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
74+
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
75+
}
76+
4777
public static void assertEqualAndSameHashCode(Object left, Object right) {
4878

4979
assertThat(left, is(right));

src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderUnitTest.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import static org.hamcrest.Matchers.*;
2020
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
2121

22+
import java.io.IOException;
2223
import java.lang.reflect.Method;
2324
import java.util.Arrays;
2425
import java.util.List;
2526
import java.util.Optional;
2627

28+
import javax.servlet.ServletException;
29+
2730
import org.hamcrest.Matchers;
2831
import org.junit.Rule;
2932
import org.junit.Test;
@@ -162,6 +165,8 @@ public void usesForwardedHostAsHostIfHeaderIsSet() {
162165

163166
request.addHeader("X-Forwarded-Host", "somethingDifferent");
164167

168+
adaptRequestFromForwardedHeaders();
169+
165170
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
166171
assertThat(link.getHref(), startsWith("http://somethingDifferent"));
167172
}
@@ -174,6 +179,8 @@ public void usesForwardedSslIfHeaderIsSet() {
174179

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

182+
adaptRequestFromForwardedHeaders();
183+
177184
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
178185
assertThat(link.getHref(), startsWith("https://"));
179186
}
@@ -186,6 +193,8 @@ public void usesForwardedSslIfHeaderIsSetOff() {
186193

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

196+
adaptRequestFromForwardedHeaders();
197+
189198
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
190199
assertThat(link.getHref(), startsWith("http://"));
191200
}
@@ -199,6 +208,8 @@ public void usesForwardedSslAndHostIfHeaderIsSet() {
199208
request.addHeader("X-Forwarded-Host", "somethingDifferent");
200209
request.addHeader("X-Forwarded-Ssl", "on");
201210

211+
adaptRequestFromForwardedHeaders();
212+
202213
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
203214
assertThat(link.getHref(), startsWith("https://somethingDifferent"));
204215
}
@@ -270,6 +281,8 @@ public void usesForwardedHostAndPortFromHeader() {
270281

271282
request.addHeader("X-Forwarded-Host", "foobar:8088");
272283

284+
adaptRequestFromForwardedHeaders();
285+
273286
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
274287
assertThat(link.getHref(), startsWith("http://foobar:8088"));
275288
}
@@ -282,6 +295,8 @@ public void usesFirstHostOfXForwardedHost() {
282295

283296
request.addHeader("X-Forwarded-Host", "barfoo:8888, localhost:8088");
284297

298+
adaptRequestFromForwardedHeaders();
299+
285300
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
286301
assertThat(link.getHref(), startsWith("http://barfoo:8888"));
287302
}
@@ -335,6 +350,8 @@ public void usesForwardedPortFromHeader() {
335350
request.addHeader("X-Forwarded-Port", "9090");
336351
request.setServerPort(8080);
337352

353+
adaptRequestFromForwardedHeaders();
354+
338355
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
339356

340357
assertThat(link.getHref(), startsWith("http://foobarhost:9090/"));
@@ -349,6 +366,8 @@ public void usesForwardedHostFromHeaderWithDefaultPort() {
349366
request.addHeader("X-Forwarded-Host", "foobarhost");
350367
request.setServerPort(8080);
351368

369+
adaptRequestFromForwardedHeaders();
370+
352371
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
353372
assertThat(link.getHref(), startsWith("http://foobarhost/"));
354373
}
@@ -410,12 +429,14 @@ public void usesRootMappingOfTargetClassForMethodsOfParent() throws Exception {
410429
* @see #257, #107
411430
*/
412431
@Test
413-
public void usesXForwardedProtoHeaderAsLinkSchema() {
432+
public void usesXForwardedProtoHeaderAsLinkSchema() throws ServletException, IOException {
414433

415434
for (String proto : Arrays.asList("http", "https")) {
416435

417436
setUp();
418437
request.addHeader("X-Forwarded-Proto", proto);
438+
439+
adaptRequestFromForwardedHeaders();
419440

420441
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
421442
assertThat(link.getHref(), startsWith(proto + "://"));
@@ -433,6 +454,8 @@ public void usesProtoValueFromForwardedHeaderAsLinkSchema() {
433454
setUp();
434455
request.addHeader("Forwarded", new String[] { "proto=" + proto });
435456

457+
adaptRequestFromForwardedHeaders();
458+
436459
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
437460
assertThat(link.getHref(), startsWith(proto.concat("://")));
438461
}
@@ -445,7 +468,9 @@ public void usesProtoValueFromForwardedHeaderAsLinkSchema() {
445468
public void favorsStandardForwardHeaderOverXForwardedProto() {
446469

447470
request.addHeader("X-Forwarded-Proto", "foo");
448-
request.addHeader(ForwardedHeader.NAME, "proto=bar");
471+
request.addHeader("Forwarded", "proto=bar");
472+
473+
adaptRequestFromForwardedHeaders();
449474

450475
Link link = linkTo(PersonControllerImpl.class).withSelfRel();
451476
assertThat(link.getHref(), startsWith("bar://"));
@@ -527,6 +552,8 @@ public void supportsTwoProxiesAddingXForwardedPort() {
527552
request.addHeader("X-Forwarded-Port", "1443,8443");
528553
request.addHeader("X-Forwarded-Host", "proxy1,proxy2");
529554

555+
adaptRequestFromForwardedHeaders();
556+
530557
assertThat(linkTo(PersonControllerImpl.class).withSelfRel().getHref(), startsWith("http://proxy1:1443"));
531558
}
532559

0 commit comments

Comments
 (0)