Forwarded header auto-config should be conditional on missing bean

See gh-5677
pull/16554/head
Madhura Bhave 6 years ago
parent 959e161555
commit 62ec8f373a

@ -25,6 +25,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
@ -65,6 +66,7 @@ public class ReactiveWebServerFactoryAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "server.forward-headers-strategy", @ConditionalOnProperty(value = "server.forward-headers-strategy",
havingValue = "framework") havingValue = "framework")
public ForwardedHeaderTransformer forwardedHeaderTransformer() { public ForwardedHeaderTransformer forwardedHeaderTransformer() {

@ -79,6 +79,7 @@ public class ServletWebServerFactoryAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
@ConditionalOnProperty(value = "server.forward-headers-strategy", @ConditionalOnProperty(value = "server.forward-headers-strategy",
havingValue = "framework") havingValue = "framework")
public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() { public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {

@ -165,6 +165,16 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
.doesNotHaveBean(ForwardedHeaderTransformer.class)); .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) @Configuration(proxyBeanMethods = false)
protected static class HttpHandlerConfiguration { protected static class HttpHandlerConfiguration {
@ -238,4 +248,14 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class ForwardedHeaderTransformerConfiguration {
@Bean
public ForwardedHeaderTransformer testForwardedHeaderTransformer() {
return new ForwardedHeaderTransformer();
}
}
} }

@ -208,6 +208,14 @@ public class ServletWebServerFactoryAutoConfigurationTests {
.doesNotHaveBean(FilterRegistrationBean.class)); .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<AssertableWebApplicationContext> verifyContext() { private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
return this::verifyContext; return this::verifyContext;
} }
@ -357,4 +365,15 @@ public class ServletWebServerFactoryAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class ForwardedHeaderFilterConfiguration {
@Bean
public FilterRegistrationBean<ForwardedHeaderFilter> testForwardedHeaderFilter() {
ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
return new FilterRegistrationBean<>(filter);
}
}
} }

Loading…
Cancel
Save