diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java index af56ac60ad6..0d064facf0f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java @@ -210,6 +210,23 @@ public class FilterRegistrationBean extends RegistrationBean { } } + /** + * Convenience method to {@link #setDispatcherTypes(EnumSet) set dispatcher types} + * using the specified elements. + */ + public void setDispatcherTypes(DispatcherType first, DispatcherType... rest) { + this.dispatcherTypes = EnumSet.of(first, rest); + } + + /** + * Sets the dispatcher types that should be used with the registration. If not + * specified the types will be deduced based on the value of + * {@link #isAsyncSupported()}. + */ + public void setDispatcherTypes(EnumSet dispatcherTypes) { + this.dispatcherTypes = dispatcherTypes; + } + /** * Set if the filter mappings should be matched after any declared filter mappings of * the ServletContext. Defaults to {@code false} indicating the filters are supposed diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java index b92147c554e..155488152b3 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java @@ -18,10 +18,12 @@ package org.springframework.boot.context.embedded; import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; +import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; @@ -222,6 +224,25 @@ public class FilterRegistrationBeanTests { bean.addServletNames((String[]) null); } + @Test + public void withSpecificDispatcherTypes() throws Exception { + FilterRegistrationBean bean = new FilterRegistrationBean(this.filter); + bean.setDispatcherTypes(DispatcherType.INCLUDE, DispatcherType.FORWARD); + bean.onStartup(this.servletContext); + verify(this.registration).addMappingForUrlPatterns( + EnumSet.of(DispatcherType.INCLUDE, DispatcherType.FORWARD), false, "/*"); + } + + @Test + public void withSpecificDispatcherTypesEnumSet() throws Exception { + FilterRegistrationBean bean = new FilterRegistrationBean(this.filter); + EnumSet types = EnumSet.of(DispatcherType.INCLUDE, + DispatcherType.FORWARD); + bean.setDispatcherTypes(types); + bean.onStartup(this.servletContext); + verify(this.registration).addMappingForUrlPatterns(types, false, "/*"); + } + private ServletRegistrationBean mockServletRegistation(String name) { ServletRegistrationBean bean = new ServletRegistrationBean(); bean.setName(name);