Add support for useRelativeRedirects customization
Closes gh-10597
This commit is contained in:
@@ -600,6 +600,12 @@ public class ServerProperties {
|
||||
*/
|
||||
private Boolean redirectContextRoot;
|
||||
|
||||
/**
|
||||
* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
|
||||
* will use relative or absolute redirects.
|
||||
*/
|
||||
private Boolean useRelativeRedirects;
|
||||
|
||||
/**
|
||||
* Character encoding to use to decode the URI.
|
||||
*/
|
||||
@@ -714,6 +720,14 @@ public class ServerProperties {
|
||||
this.redirectContextRoot = redirectContextRoot;
|
||||
}
|
||||
|
||||
public Boolean getUseRelativeRedirects() {
|
||||
return this.useRelativeRedirects;
|
||||
}
|
||||
|
||||
public void setUseRelativeRedirects(Boolean useRelativeRedirects) {
|
||||
this.useRelativeRedirects = useRelativeRedirects;
|
||||
}
|
||||
|
||||
public String getRemoteIpHeader() {
|
||||
return this.remoteIpHeader;
|
||||
}
|
||||
|
||||
@@ -256,6 +256,10 @@ public class DefaultServletWebServerFactoryCustomizer
|
||||
customizeRedirectContextRoot(factory,
|
||||
tomcatProperties.getRedirectContextRoot());
|
||||
}
|
||||
if (tomcatProperties.getUseRelativeRedirects() != null) {
|
||||
customizeUseRelativeRedirects(factory,
|
||||
tomcatProperties.getUseRelativeRedirects());
|
||||
}
|
||||
if (tomcatProperties.getMaxConnections() > 0) {
|
||||
customizeMaxConnections(factory, tomcatProperties.getMaxConnections());
|
||||
}
|
||||
@@ -392,6 +396,13 @@ public class DefaultServletWebServerFactoryCustomizer
|
||||
.setMapperContextRootRedirectEnabled(redirectContextRoot));
|
||||
}
|
||||
|
||||
private static void customizeUseRelativeRedirects(
|
||||
TomcatServletWebServerFactory factory,
|
||||
final boolean useRelativeRedirects) {
|
||||
factory.addContextCustomizers((context) -> context
|
||||
.setUseRelativeRedirects(useRelativeRedirects));
|
||||
}
|
||||
|
||||
private static void customizeStaticResources(Resource resource,
|
||||
TomcatServletWebServerFactory factory) {
|
||||
if (resource.getCacheTtl() == null) {
|
||||
|
||||
@@ -168,6 +168,22 @@ public class DefaultServletWebServerFactoryCustomizerTests {
|
||||
verify(context).setMapperContextRootRedirectEnabled(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useRelativeRedirectsCanBeConfigured() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.use-relative-redirects", "true");
|
||||
bindProperties(map);
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertThat(tomcat.getUseRelativeRedirects()).isTrue();
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
Context context = mock(Context.class);
|
||||
for (TomcatContextCustomizer customizer : factory.getTomcatContextCustomizers()) {
|
||||
customizer.customize(context);
|
||||
}
|
||||
verify(context).setUseRelativeRedirects(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcat() throws Exception {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
|
||||
Reference in New Issue
Block a user