diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java index 72ead1b2cf..cdf9777616 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.ServerProperties; @@ -65,6 +66,7 @@ public class ReactiveWebServerFactoryAutoConfiguration { } @Bean + @ConditionalOnMissingBean @ConditionalOnProperty(value = "server.forward-headers-strategy", havingValue = "framework") public ForwardedHeaderTransformer forwardedHeaderTransformer() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java index 642b427cfd..1fe9aa1194 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java @@ -79,6 +79,7 @@ public class ServletWebServerFactoryAutoConfiguration { } @Bean + @ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class) @ConditionalOnProperty(value = "server.forward-headers-strategy", havingValue = "framework") public FilterRegistrationBean forwardedHeaderFilter() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java index fb71b10c9a..f3efd04152 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java @@ -165,6 +165,16 @@ public class ReactiveWebServerFactoryAutoConfigurationTests { .doesNotHaveBean(ForwardedHeaderTransformer.class)); } + @Test + public void forwardedHeaderTransformerWhenAlreadyRegisteredShouldBackOff() { + this.contextRunner + .withUserConfiguration(ForwardedHeaderTransformerConfiguration.class, + HttpHandlerConfiguration.class) + .withPropertyValues("server.forward-headers-strategy=framework") + .run((context) -> assertThat(context) + .hasSingleBean(ForwardedHeaderTransformer.class)); + } + @Configuration(proxyBeanMethods = false) protected static class HttpHandlerConfiguration { @@ -238,4 +248,14 @@ public class ReactiveWebServerFactoryAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class ForwardedHeaderTransformerConfiguration { + + @Bean + public ForwardedHeaderTransformer testForwardedHeaderTransformer() { + return new ForwardedHeaderTransformer(); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java index d070725c8b..6c62abb5f4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java @@ -208,6 +208,14 @@ public class ServletWebServerFactoryAutoConfigurationTests { .doesNotHaveBean(FilterRegistrationBean.class)); } + @Test + public void forwardedHeaderFilterWhenFilterAlreadyRegisteredShouldBackOff() { + this.contextRunner.withUserConfiguration(ForwardedHeaderFilterConfiguration.class) + .withPropertyValues("server.forward-headers-strategy=framework") + .run((context) -> assertThat(context) + .hasSingleBean(FilterRegistrationBean.class)); + } + private ContextConsumer verifyContext() { return this::verifyContext; } @@ -357,4 +365,15 @@ public class ServletWebServerFactoryAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class ForwardedHeaderFilterConfiguration { + + @Bean + public FilterRegistrationBean testForwardedHeaderFilter() { + ForwardedHeaderFilter filter = new ForwardedHeaderFilter(); + return new FilterRegistrationBean<>(filter); + } + + } + }