File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
main/java/org/springframework/hateoas/mvc
test/java/org/springframework/hateoas/mvc Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -158,7 +158,8 @@ public UriComponentsBuilder toUriComponentsBuilder() {
158
158
159
159
/**
160
160
* Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the
161
- * request contains an {@code X-Forwarded-Host} header.
161
+ * request contains an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an
162
+ * {@code X-Forwarded-Ssl} header
162
163
*
163
164
* @return
164
165
*/
@@ -167,6 +168,12 @@ static UriComponentsBuilder getBuilder() {
167
168
HttpServletRequest request = getCurrentRequest ();
168
169
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder .fromServletMapping (request );
169
170
171
+ String forwardedSsl = request .getHeader ("X-Forwarded-Ssl" );
172
+
173
+ if (StringUtils .hasText (forwardedSsl ) && forwardedSsl .equalsIgnoreCase ("on" )) {
174
+ builder .scheme ("https" );
175
+ }
176
+
170
177
String header = request .getHeader ("X-Forwarded-Host" );
171
178
172
179
if (!StringUtils .hasText (header )) {
Original file line number Diff line number Diff line change @@ -149,6 +149,43 @@ public void usesForwardedHostAsHostIfHeaderIsSet() {
149
149
assertThat (link .getHref (), startsWith ("http://somethingDifferent" ));
150
150
}
151
151
152
+ /**
153
+ * @see #112
154
+ */
155
+ @ Test
156
+ public void usesForwardedSslIfHeaderIsSet () {
157
+
158
+ request .addHeader ("X-Forwarded-Ssl" , "on" );
159
+
160
+ Link link = linkTo (PersonControllerImpl .class ).withSelfRel ();
161
+ assertThat (link .getHref (), startsWith ("https://" ));
162
+ }
163
+
164
+ /**
165
+ * @see #112
166
+ */
167
+ @ Test
168
+ public void usesForwardedSslIfHeaderIsSetOff () {
169
+
170
+ request .addHeader ("X-Forwarded-Ssl" , "off" );
171
+
172
+ Link link = linkTo (PersonControllerImpl .class ).withSelfRel ();
173
+ assertThat (link .getHref (), startsWith ("http://" ));
174
+ }
175
+
176
+ /**
177
+ * @see #112
178
+ */
179
+ @ Test
180
+ public void usesForwardedSslAndHostIfHeaderIsSet () {
181
+
182
+ request .addHeader ("X-Forwarded-Host" , "somethingDifferent" );
183
+ request .addHeader ("X-Forwarded-Ssl" , "on" );
184
+
185
+ Link link = linkTo (PersonControllerImpl .class ).withSelfRel ();
186
+ assertThat (link .getHref (), startsWith ("https://somethingDifferent" ));
187
+ }
188
+
152
189
/**
153
190
* @see #26, #39
154
191
*/
You can’t perform that action at this time.
0 commit comments