From f8e08f79ed2b032071589a3503d053117f2127cc Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 24 Aug 2017 10:28:59 +0200 Subject: [PATCH] Add DispatcherType enum for configuration purpose This commit creates our own version of javax.servlet.DispatcherType for configuration purpose as we can use the other one now the servlet api is optional. Closes gh-9856 --- .../SecurityFilterAutoConfiguration.java | 12 ++--- .../security/SecurityProperties.java | 9 ++-- .../session/SessionProperties.java | 9 ++-- .../SessionRepositoryFilterConfiguration.java | 13 ++--- ...itional-spring-configuration-metadata.json | 8 +++ .../appendix-application-properties.adoc | 2 +- .../boot/web/servlet/DispatcherType.java | 54 +++++++++++++++++++ 7 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/web/servlet/DispatcherType.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityFilterAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityFilterAutoConfiguration.java index 07b71541f8..bfa6e6ccb9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityFilterAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityFilterAutoConfiguration.java @@ -17,8 +17,7 @@ package org.springframework.boot.autoconfigure.security; import java.util.EnumSet; -import java.util.HashSet; -import java.util.Set; +import java.util.stream.Collectors; import javax.servlet.DispatcherType; @@ -73,11 +72,10 @@ public class SecurityFilterAutoConfiguration { if (securityProperties.getFilterDispatcherTypes() == null) { return null; } - Set dispatcherTypes = new HashSet<>(); - for (String dispatcherType : securityProperties.getFilterDispatcherTypes()) { - dispatcherTypes.add(DispatcherType.valueOf(dispatcherType)); - } - return EnumSet.copyOf(dispatcherTypes); + return securityProperties.getFilterDispatcherTypes().stream() + .map((type) -> DispatcherType.valueOf(type.name())) + .collect(Collectors + .collectingAndThen(Collectors.toSet(), EnumSet::copyOf)); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java index 9adce8b76b..7deecdb97a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.UUID; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.web.servlet.DispatcherType; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.core.Ordered; import org.springframework.security.config.http.SessionCreationPolicy; @@ -104,8 +105,8 @@ public class SecurityProperties implements SecurityPrerequisite { /** * Security filter chain dispatcher types. */ - private Set filterDispatcherTypes = new HashSet<>( - Arrays.asList("ASYNC", "ERROR", "REQUEST")); + private Set filterDispatcherTypes = new HashSet<>(Arrays.asList( + DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST)); public Headers getHeaders() { return this.headers; @@ -163,11 +164,11 @@ public class SecurityProperties implements SecurityPrerequisite { this.filterOrder = filterOrder; } - public Set getFilterDispatcherTypes() { + public Set getFilterDispatcherTypes() { return this.filterDispatcherTypes; } - public void setFilterDispatcherTypes(Set filterDispatcherTypes) { + public void setFilterDispatcherTypes(Set filterDispatcherTypes) { this.filterDispatcherTypes = filterDispatcherTypes; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java index 70bd816988..e001b17e8e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java @@ -23,6 +23,7 @@ import java.util.Set; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.web.servlet.DispatcherType; import org.springframework.session.web.http.SessionRepositoryFilter; /** @@ -88,8 +89,8 @@ public class SessionProperties { /** * Session repository filter dispatcher types. */ - private Set filterDispatcherTypes = new HashSet<>( - Arrays.asList("ASYNC", "ERROR", "REQUEST")); + private Set filterDispatcherTypes = new HashSet<>(Arrays.asList( + DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST)); public int getFilterOrder() { return this.filterOrder; @@ -99,11 +100,11 @@ public class SessionProperties { this.filterOrder = filterOrder; } - public Set getFilterDispatcherTypes() { + public Set getFilterDispatcherTypes() { return this.filterDispatcherTypes; } - public void setFilterDispatcherTypes(Set filterDispatcherTypes) { + public void setFilterDispatcherTypes(Set filterDispatcherTypes) { this.filterDispatcherTypes = filterDispatcherTypes; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.java index 9f6ee6026f..1be07609eb 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.java @@ -17,8 +17,6 @@ package org.springframework.boot.autoconfigure.session; import java.util.EnumSet; -import java.util.Locale; -import java.util.Set; import java.util.stream.Collectors; import javax.servlet.DispatcherType; @@ -52,14 +50,13 @@ class SessionRepositoryFilterConfiguration { private EnumSet getDispatcherTypes( SessionProperties sessionProperties) { - Set filterDispatcherTypes = sessionProperties.getServlet() - .getFilterDispatcherTypes(); - if (filterDispatcherTypes == null) { + SessionProperties.Servlet servletProperties = sessionProperties.getServlet(); + if (servletProperties.getFilterDispatcherTypes() == null) { return null; } - return filterDispatcherTypes.stream() - .map((type) -> type.toUpperCase(Locale.ENGLISH)) - .map(DispatcherType::valueOf).collect(Collectors + return servletProperties.getFilterDispatcherTypes().stream() + .map((type) -> DispatcherType.valueOf(type.name())) + .collect(Collectors .collectingAndThen(Collectors.toSet(), EnumSet::copyOf)); } diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 04bf53776c..f0aa6ccc19 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -19,6 +19,10 @@ "name": "security.headers.hsts", "defaultValue": "all" }, + { + "name": "security.filter-dispatcher-types", + "defaultValue": ["async", "error", "request"] + }, { "name": "security.sessions", "defaultValue": "stateless" @@ -379,6 +383,10 @@ "name": "spring.session.hazelcast.flush-mode", "defaultValue": "on-save" }, + { + "name": "spring.session.servlet.filter-dispatcher-types", + "defaultValue": ["async", "error", "request"] + }, { "name": "spring.session.redis.flush-mode", "defaultValue": "on-save" diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 379f471122..7457dd7778 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -476,7 +476,7 @@ content into your application; rather pick only the properties that you need. security.basic.realm=Spring # HTTP basic realm name. security.enable-csrf=false # Enable Cross Site Request Forgery support. security.filter-order=0 # Security filter chain order. - security.filter-dispatcher-types=ASYNC, FORWARD, INCLUDE, REQUEST # Security filter chain dispatcher types. + security.filter-dispatcher-types=ASYNC,ERROR,REQUEST # Security filter chain dispatcher types. security.headers.cache=true # Enable cache control HTTP headers. security.headers.content-security-policy= # Value for content security policy header. security.headers.content-security-policy-mode=default # Content security policy mode. diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/DispatcherType.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/DispatcherType.java new file mode 100644 index 0000000000..e01538bbd7 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/DispatcherType.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.servlet; + +/** + * Enumeration of filter dispatcher types, identical to + * {@link javax.servlet.DispatcherType} and used in configuration as the servlet api may + * not be present. + * + * @author Stephane Nicoll + * @since 2.0.0 + */ +public enum DispatcherType { + + /** + * Apply the filter on "RequestDispatcher.forward()" calls. + */ + FORWARD, + + /** + * Apply the filter on "RequestDispatcher.include()" calls. + */ + INCLUDE, + + /** + * Apply the filter on ordinary client calls. + */ + REQUEST, + + /** + * Apply the filter under calls dispatched from an AsyncContext. + */ + ASYNC, + + /** + * Apply the filter when an error is handled. + */ + ERROR + +}