Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relative redirects is automatically enabled when `server.forward-head…
Browse files Browse the repository at this point in the history
…ers-strategy` is configured as `framework`

Fixes gh-27801
terminux committed Jan 12, 2022
1 parent ba3842b commit 41ef3b9
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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.
@@ -90,6 +90,7 @@ public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCust
@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);
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.filter.ForwardedHeaderFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
@@ -348,11 +349,14 @@ void tomcatProtocolHandlerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnc
}

@Test
void forwardedHeaderFilterShouldBeConfigured() {
void forwardedHeaderFilterShouldBeConfiguredAndUseRelativeRedirects() {
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();
});
}

0 comments on commit 41ef3b9

Please sign in to comment.