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
pull/10069/head
Stephane Nicoll 7 years ago
parent 8383648a14
commit f8e08f79ed

@ -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<DispatcherType> 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));
}
}

@ -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<String> filterDispatcherTypes = new HashSet<>(
Arrays.asList("ASYNC", "ERROR", "REQUEST"));
private Set<DispatcherType> 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<String> getFilterDispatcherTypes() {
public Set<DispatcherType> getFilterDispatcherTypes() {
return this.filterDispatcherTypes;
}
public void setFilterDispatcherTypes(Set<String> filterDispatcherTypes) {
public void setFilterDispatcherTypes(Set<DispatcherType> filterDispatcherTypes) {
this.filterDispatcherTypes = filterDispatcherTypes;
}

@ -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<String> filterDispatcherTypes = new HashSet<>(
Arrays.asList("ASYNC", "ERROR", "REQUEST"));
private Set<DispatcherType> 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<String> getFilterDispatcherTypes() {
public Set<DispatcherType> getFilterDispatcherTypes() {
return this.filterDispatcherTypes;
}
public void setFilterDispatcherTypes(Set<String> filterDispatcherTypes) {
public void setFilterDispatcherTypes(Set<DispatcherType> filterDispatcherTypes) {
this.filterDispatcherTypes = filterDispatcherTypes;
}

@ -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<DispatcherType> getDispatcherTypes(
SessionProperties sessionProperties) {
Set<String> 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));
}

@ -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"

@ -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.

@ -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
}
Loading…
Cancel
Save