Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: spring-projects/spring-boot
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 41ef3b92a78a5d91b1899ab5a10a0e7eb2cd52c4
Choose a base ref
..
head repository: spring-projects/spring-boot
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9015cfd01ccce8561a82783abe2197bb1e1c176d
Choose a head ref
Original file line number Diff line number Diff line change
@@ -85,16 +85,33 @@ public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCust
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
}

@Bean
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "server.forward-headers-strategy", havingValue = "framework")
public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {
ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
filter.setRelativeRedirects(true);
FilterRegistrationBean<ForwardedHeaderFilter> registration = new FilterRegistrationBean<>(filter);
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registration;
static class ForwardedHeaderFilterConfiguration {

@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
public FilterRegistrationBean<ForwardedHeaderFilter> tomcatForwardedHeaderFilter(
ServerProperties serverProperties) {
return createForwardedHeaderFilter(serverProperties.getTomcat().isUseRelativeRedirects());
}

@Bean
@ConditionalOnMissingFilterBean(ForwardedHeaderFilter.class)
public FilterRegistrationBean<ForwardedHeaderFilter> defaultForwardedHeaderFilter() {
return createForwardedHeaderFilter(false);
}

private FilterRegistrationBean<ForwardedHeaderFilter> createForwardedHeaderFilter(boolean relativeRedirects) {
ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
filter.setRelativeRedirects(relativeRedirects);
FilterRegistrationBean<ForwardedHeaderFilter> registration = new FilterRegistrationBean<>(filter);
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registration;
}

}

/**
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -349,14 +349,11 @@ void tomcatProtocolHandlerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnc
}

@Test
void forwardedHeaderFilterShouldBeConfiguredAndUseRelativeRedirects() {
void forwardedHeaderFilterShouldBeConfigured() {
this.contextRunner.withPropertyValues("server.forward-headers-strategy=framework").run((context) -> {
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
assertThat(filter).isInstanceOf(ForwardedHeaderFilter.class);
Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, Boolean.class,
"relativeRedirects");
assertThat(relativeRedirects).isTrue();
});
}

@@ -373,6 +370,36 @@ void forwardedHeaderFilterWhenFilterAlreadyRegisteredShouldBackOff() {
.run((context) -> assertThat(context).hasSingleBean(FilterRegistrationBean.class));
}

@Test
void relativeRedirectsShouldBeEnabledWhenUsingTomcatContainerAndUseRelativeRedirects() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class))
.withPropertyValues("server.forward-headers-strategy=framework",
"server.tomcat.use-relative-redirects=true");

runner.run((context) -> {
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects");
assertThat(relativeRedirects).isTrue();
});
}

@Test
void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withClassLoader(new FilteredClassLoader(Tomcat.class))
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class))
.withPropertyValues("server.forward-headers-strategy=framework");

runner.run((context) -> {
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects");
assertThat(relativeRedirects).isFalse();
});
}

private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
return this::verifyContext;
}