Skip to content

Commit 9015cfd

Browse files
committed
Fix the bug that the use-relative-redirects configuration item of tomcat does not take effect
Fixes gh-27801
1 parent ba3842b commit 9015cfd

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -85,15 +85,33 @@ public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCust
8585
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
8686
}
8787

88-
@Bean
89-
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
88+
@Configuration(proxyBeanMethods = false)
9089
@ConditionalOnProperty(value = "server.forward-headers-strategy", havingValue = "framework")
91-
public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {
92-
ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
93-
FilterRegistrationBean<ForwardedHeaderFilter> registration = new FilterRegistrationBean<>(filter);
94-
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR);
95-
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
96-
return registration;
90+
static class ForwardedHeaderFilterConfiguration {
91+
92+
@Bean
93+
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
94+
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
95+
public FilterRegistrationBean<ForwardedHeaderFilter> tomcatForwardedHeaderFilter(
96+
ServerProperties serverProperties) {
97+
return createForwardedHeaderFilter(serverProperties.getTomcat().isUseRelativeRedirects());
98+
}
99+
100+
@Bean
101+
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
102+
public FilterRegistrationBean<ForwardedHeaderFilter> defaultForwardedHeaderFilter() {
103+
return createForwardedHeaderFilter(false);
104+
}
105+
106+
private FilterRegistrationBean<ForwardedHeaderFilter> createForwardedHeaderFilter(boolean relativeRedirects) {
107+
ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
108+
filter.setRelativeRedirects(relativeRedirects);
109+
FilterRegistrationBean<ForwardedHeaderFilter> registration = new FilterRegistrationBean<>(filter);
110+
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR);
111+
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
112+
return registration;
113+
}
114+
97115
}
98116

99117
/**

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -57,6 +57,7 @@
5757
import org.springframework.context.annotation.Bean;
5858
import org.springframework.context.annotation.Configuration;
5959
import org.springframework.stereotype.Component;
60+
import org.springframework.test.util.ReflectionTestUtils;
6061
import org.springframework.web.filter.ForwardedHeaderFilter;
6162
import org.springframework.web.servlet.DispatcherServlet;
6263
import org.springframework.web.servlet.FrameworkServlet;
@@ -369,6 +370,36 @@ void forwardedHeaderFilterWhenFilterAlreadyRegisteredShouldBackOff() {
369370
.run((context) -> assertThat(context).hasSingleBean(FilterRegistrationBean.class));
370371
}
371372

373+
@Test
374+
void relativeRedirectsShouldBeEnabledWhenUsingTomcatContainerAndUseRelativeRedirects() {
375+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
376+
AnnotationConfigServletWebServerApplicationContext::new)
377+
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class))
378+
.withPropertyValues("server.forward-headers-strategy=framework",
379+
"server.tomcat.use-relative-redirects=true");
380+
381+
runner.run((context) -> {
382+
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
383+
Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects");
384+
assertThat(relativeRedirects).isTrue();
385+
});
386+
}
387+
388+
@Test
389+
void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() {
390+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
391+
AnnotationConfigServletWebServerApplicationContext::new)
392+
.withClassLoader(new FilteredClassLoader(Tomcat.class))
393+
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class))
394+
.withPropertyValues("server.forward-headers-strategy=framework");
395+
396+
runner.run((context) -> {
397+
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
398+
Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects");
399+
assertThat(relativeRedirects).isFalse();
400+
});
401+
}
402+
372403
private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
373404
return this::verifyContext;
374405
}

0 commit comments

Comments
 (0)