From 64f04fceea461317e2afc04d66ff0e50140e28ed Mon Sep 17 00:00:00 2001 From: artsiom Date: Sat, 22 Sep 2018 19:48:45 +0300 Subject: [PATCH] Make sure cache busting works with error pages See gh-14583 --- .../thymeleaf/ThymeleafAutoConfiguration.java | 11 +++++++++-- .../ThymeleafServletAutoConfigurationTests.java | 15 +++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 97c9d783a6..e7deacdcbe 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import javax.annotation.PostConstruct; +import javax.servlet.DispatcherType; import com.github.mxab.thymeleaf.extras.dataattribute.dialect.DataAttributeDialect; import nz.net.ultraq.thymeleaf.LayoutDialect; @@ -53,6 +54,7 @@ import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfigurat import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.PropertyMapper; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -70,6 +72,7 @@ import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; * @author Eddú Meléndez * @author Daniel Fernández * @author Kazuki Shimizu + * @author Artsiom Yudovin */ @Configuration @EnableConfigurationProperties(ThymeleafProperties.class) @@ -166,8 +169,12 @@ public class ThymeleafAutoConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledResourceChain - public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { - return new ResourceUrlEncodingFilter(); + public FilterRegistrationBean resourceUrlEncodingFilter() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( + new ResourceUrlEncodingFilter()); + filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, + DispatcherType.ERROR); + return filterRegistrationBean; } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index e482fecdce..7a226aaef6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -18,8 +18,11 @@ package org.springframework.boot.autoconfigure.thymeleaf; import java.io.File; import java.util.Collections; +import java.util.EnumSet; import java.util.Locale; +import javax.servlet.DispatcherType; + import nz.net.ultraq.thymeleaf.LayoutDialect; import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; import org.junit.After; @@ -37,6 +40,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.rule.OutputCapture; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -47,7 +51,6 @@ import org.springframework.mock.web.MockServletContext; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.support.RequestContext; import static org.assertj.core.api.Assertions.assertThat; @@ -61,6 +64,7 @@ import static org.hamcrest.Matchers.containsString; * @author Eddú Meléndez * @author Brian Clozel * @author Kazuki Shimizu + * @author Artsiom Yudovin */ public class ThymeleafServletAutoConfigurationTests { @@ -205,14 +209,17 @@ public class ThymeleafServletAutoConfigurationTests { @Test public void registerResourceHandlingFilterDisabledByDefault() { load(BaseConfiguration.class); - assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class)) - .isEmpty(); + assertThat(this.context.getBeansOfType(FilterRegistrationBean.class)).isEmpty(); } @Test public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() { load(BaseConfiguration.class, "spring.resources.chain.enabled:true"); - assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull(); + FilterRegistrationBean registration = this.context + .getBean(FilterRegistrationBean.class); + assertThat(registration).isNotNull(); + assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes", + EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR)); } @Test