Skip to content

Commit f29bce6

Browse files
committed
Harmonize default for server.tomcat.use-relative-redirects
Prior to this commit, the property was a Boolean with a null default. If it was explicitly set by the user, a context customizer would use that value to set it on the context. However, if it was not set, the default wouldn't be tomcat's default but `false` because it was explicitly set to `false` in `TomcatServletWebServerFactory`. This commit defaults the property itself to `false` so that the default is more obvious to the user. Fixes gh-20796
1 parent 386d678 commit f29bce6

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public static class Tomcat {
380380
* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
381381
* will use relative or absolute redirects.
382382
*/
383-
private Boolean useRelativeRedirects;
383+
private boolean useRelativeRedirects;
384384

385385
/**
386386
* Character encoding to use to decode the URI.
@@ -537,14 +537,19 @@ public void setRedirectContextRoot(Boolean redirectContextRoot) {
537537
this.redirectContextRoot = redirectContextRoot;
538538
}
539539

540-
public Boolean getUseRelativeRedirects() {
540+
public boolean getUseRelativeRedirects() {
541541
return this.useRelativeRedirects;
542542
}
543543

544-
public void setUseRelativeRedirects(Boolean useRelativeRedirects) {
544+
public void setUseRelativeRedirects(boolean useRelativeRedirects) {
545545
this.useRelativeRedirects = useRelativeRedirects;
546546
}
547547

548+
@Deprecated
549+
public void setUseRelativeRedirects(Boolean useRelativeRedirects) {
550+
this.useRelativeRedirects = (useRelativeRedirects != null) ? useRelativeRedirects : false;
551+
}
552+
548553
public String getRemoteIpHeader() {
549554
return this.remoteIpHeader;
550555
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/TomcatServletWebServerFactoryCustomizer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ public void customize(TomcatServletWebServerFactory factory) {
5454
if (tomcatProperties.getRedirectContextRoot() != null) {
5555
customizeRedirectContextRoot(factory, tomcatProperties.getRedirectContextRoot());
5656
}
57-
if (tomcatProperties.getUseRelativeRedirects() != null) {
58-
customizeUseRelativeRedirects(factory, tomcatProperties.getUseRelativeRedirects());
59-
}
57+
customizeUseRelativeRedirects(factory, tomcatProperties.getUseRelativeRedirects());
6058
factory.setDisableMBeanRegistry(!tomcatProperties.getMbeanregistry().isEnabled());
6159
}
6260

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void testTomcatBinding() {
131131
map.put("server.tomcat.background-processor-delay", "10");
132132
map.put("server.tomcat.relaxed-path-chars", "|,<");
133133
map.put("server.tomcat.relaxed-query-chars", "^ , | ");
134+
map.put("server.tomcat.use-relative-redirects", "true");
134135
bind(map);
135136
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
136137
Accesslog accesslog = tomcat.getAccesslog();
@@ -152,6 +153,7 @@ void testTomcatBinding() {
152153
assertThat(tomcat.getBackgroundProcessorDelay()).isEqualTo(Duration.ofSeconds(10));
153154
assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');
154155
assertThat(tomcat.getRelaxedQueryChars()).containsExactly('^', '|');
156+
assertThat(tomcat.getUseRelativeRedirects()).isTrue();
155157
}
156158

157159
@Test
@@ -347,6 +349,11 @@ void tomcatInternalProxiesMatchesDefault() {
347349
.isEqualTo(new RemoteIpValve().getInternalProxies());
348350
}
349351

352+
@Test
353+
void tomcatUseRelativeRedirectsDefaultsToFalse() {
354+
assertThat(this.properties.getTomcat().getUseRelativeRedirects()).isFalse();
355+
}
356+
350357
@Test
351358
void jettyMaxHttpFormPostSizeMatchesDefault() throws Exception {
352359
JettyServletWebServerFactory jettyFactory = new JettyServletWebServerFactory(0);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
214214
: ClassUtils.getDefaultClassLoader());
215215
resetDefaultLocaleMapping(context);
216216
addLocaleMappings(context);
217-
context.setUseRelativeRedirects(false);
218217
try {
219218
context.setCreateUploadTargets(true);
220219
}

0 commit comments

Comments
 (0)