Forwarded header auto-config should be conditional on missing bean
See gh-5677
This commit is contained in:
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.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() {
|
||||
|
|
|
@ -79,6 +79,7 @@ public class ServletWebServerFactoryAutoConfiguration {
|
|||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
|
||||
@ConditionalOnProperty(value = "server.forward-headers-strategy",
|
||||
havingValue = "framework")
|
||||
public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<AssertableWebApplicationContext> 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…
Reference in New Issue