Fix ResourceUrlEncodingFilter conditions

Fix `ResourceUrlEncodingFilter` conditions which were inadvertently
changed in commits 64f04fce and 6cc272ec and would back off if any
`FilterRegistrationBean` was found.

The updated conditions restores the behavior of Spring Boot 2.0.5 and
allows users to directly register their own `ResourceUrlEncodingFilter`
beans (as long as they don't use a `FilterRegistrationBean`).

Fixes gh-14897
pull/16246/head
Phillip Webb 6 years ago
parent f145e81354
commit de7eeb5014

@ -74,7 +74,7 @@ class FreeMarkerServletWebConfiguration extends AbstractFreeMarkerConfiguration
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean(ResourceUrlEncodingFilter.class)
@ConditionalOnEnabledResourceChain
public FilterRegistrationBean<ResourceUrlEncodingFilter> resourceUrlEncodingFilter() {
FilterRegistrationBean<ResourceUrlEncodingFilter> registration = new FilterRegistrationBean<>(

@ -167,7 +167,7 @@ public class ThymeleafAutoConfiguration {
static class ThymeleafWebMvcConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean(ResourceUrlEncodingFilter.class)
@ConditionalOnEnabledResourceChain
public FilterRegistrationBean<ResourceUrlEncodingFilter> resourceUrlEncodingFilter() {
FilterRegistrationBean<ResourceUrlEncodingFilter> registration = new FilterRegistrationBean<>(

@ -19,16 +19,22 @@ package org.springframework.boot.autoconfigure.freemarker;
import java.io.StringWriter;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
@ -52,12 +58,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class FreeMarkerAutoConfigurationServletIntegrationTests {
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
@Before
public void setupContext() {
this.context.setServletContext(new MockServletContext());
}
private AnnotationConfigWebApplicationContext context;
@After
public void close() {
@ -170,9 +171,31 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
}
@Test
@SuppressWarnings("rawtypes")
public void registerResourceHandlingFilterWithOtherRegistrationBean() {
// gh-14897
registerAndRefreshContext(FilterRegistrationConfiguration.class,
"spring.resources.chain.enabled:true");
Map<String, FilterRegistrationBean> beans = this.context
.getBeansOfType(FilterRegistrationBean.class);
assertThat(beans).hasSize(2);
FilterRegistrationBean registration = beans.values().stream()
.filter((r) -> r.getFilter() instanceof ResourceUrlEncodingFilter)
.findFirst().get();
assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes",
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
}
private void registerAndRefreshContext(String... env) {
registerAndRefreshContext(BaseConfiguration.class, env);
}
private void registerAndRefreshContext(Class<?> config, String... env) {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
TestPropertyValues.of(env).applyTo(this.context);
this.context.register(FreeMarkerAutoConfiguration.class);
this.context.register(config);
this.context.refresh();
}
@ -193,4 +216,23 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
return response;
}
@Configuration
@ImportAutoConfiguration({ FreeMarkerAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
static class BaseConfiguration {
}
@Configuration
@Import(BaseConfiguration.class)
static class FilterRegistrationConfiguration {
@Bean
public FilterRegistrationBean<OrderedCharacterEncodingFilter> filterRegisration() {
return new FilterRegistrationBean<OrderedCharacterEncodingFilter>(
new OrderedCharacterEncodingFilter());
}
}
}

@ -20,6 +20,7 @@ import java.io.File;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Map;
import javax.servlet.DispatcherType;
@ -41,6 +42,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -224,6 +226,22 @@ public class ThymeleafServletAutoConfigurationTests {
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
}
@Test
@SuppressWarnings("rawtypes")
public void registerResourceHandlingFilterWithOtherRegistrationBean() {
// gh-14897
load(FilterRegistrationConfiguration.class,
"spring.resources.chain.enabled:true");
Map<String, FilterRegistrationBean> beans = this.context
.getBeansOfType(FilterRegistrationBean.class);
assertThat(beans).hasSize(2);
FilterRegistrationBean registration = beans.values().stream()
.filter((r) -> r.getFilter() instanceof ResourceUrlEncodingFilter)
.findFirst().get();
assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes",
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
}
@Test
public void layoutDialectCanBeCustomized() {
load(LayoutDialectConfiguration.class);
@ -269,4 +287,16 @@ public class ThymeleafServletAutoConfigurationTests {
}
@Configuration
@Import(BaseConfiguration.class)
static class FilterRegistrationConfiguration {
@Bean
public FilterRegistrationBean<OrderedCharacterEncodingFilter> filterRegisration() {
return new FilterRegistrationBean<OrderedCharacterEncodingFilter>(
new OrderedCharacterEncodingFilter());
}
}
}

Loading…
Cancel
Save