/*
* Copyright 2002-2020 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
*
* https://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.web.servlet.handler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper;
Helper class to get information from the HandlerMapping
that would serve a specific request. Provides the following methods:
getMatchableHandlerMapping
— obtain a HandlerMapping
to check request-matching criteria against. getCorsConfiguration
— obtain the CORS configuration for the request.
Note: This is primarily an SPI to allow Spring Security
to align its pattern matching with the same pattern matching that would be
used in Spring MVC for a given request, in order to avoid security issues.
Use of this introspector should be avoided for other purposes because it
incurs the overhead of resolving the handler for a request.
Author: Rossen Stoyanchev Since: 4.3.1
/**
* Helper class to get information from the {@code HandlerMapping} that would
* serve a specific request.
*
* <p>Provides the following methods:
* <ul>
* <li>{@link #getMatchableHandlerMapping} — obtain a {@code HandlerMapping}
* to check request-matching criteria against.
* <li>{@link #getCorsConfiguration} — obtain the CORS configuration for the
* request.
* </ul>
*
* <p><strong>Note:</strong> This is primarily an SPI to allow Spring Security
* to align its pattern matching with the same pattern matching that would be
* used in Spring MVC for a given request, in order to avoid security issues.
* Use of this introspector should be avoided for other purposes because it
* incurs the overhead of resolving the handler for a request.
*
* @author Rossen Stoyanchev
* @since 4.3.1
*/
public class HandlerMappingIntrospector
implements CorsConfigurationSource, ApplicationContextAware, InitializingBean {
@Nullable
private ApplicationContext applicationContext;
@Nullable
private List<HandlerMapping> handlerMappings;
@Nullable
private Map<HandlerMapping, MatchableHandlerMapping> pathPatternMatchableHandlerMappings =
new ConcurrentHashMap<>();
Constructor for use with ApplicationContextAware
. /**
* Constructor for use with {@link ApplicationContextAware}.
*/
public HandlerMappingIntrospector() {
}
Constructor that detects the configured HandlerMapping
s in the given ApplicationContext
or falls back on "DispatcherServlet.properties" like the DispatcherServlet
. Deprecated: as of 4.3.12, in favor of setApplicationContext
/**
* Constructor that detects the configured {@code HandlerMapping}s in the
* given {@code ApplicationContext} or falls back on
* "DispatcherServlet.properties" like the {@code DispatcherServlet}.
* @deprecated as of 4.3.12, in favor of {@link #setApplicationContext}
*/
@Deprecated
public HandlerMappingIntrospector(ApplicationContext context) {
this.handlerMappings = initHandlerMappings(context);
}
Return the configured or detected HandlerMapping's.
/**
* Return the configured or detected HandlerMapping's.
*/
public List<HandlerMapping> getHandlerMappings() {
return (this.handlerMappings != null ? this.handlerMappings : Collections.emptyList());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() {
if (this.handlerMappings == null) {
Assert.notNull(this.applicationContext, "No ApplicationContext");
this.handlerMappings = initHandlerMappings(this.applicationContext);
this.pathPatternMatchableHandlerMappings = initPathPatternMatchableHandlerMappings(this.handlerMappings);
}
}
Find the HandlerMapping
that would handle the given request and return it as a MatchableHandlerMapping
that can be used to test request-matching criteria. If the matching HandlerMapping is not an instance of MatchableHandlerMapping
, an IllegalStateException is raised.
Params: - request – the current request
Throws: - Exception – if any of the HandlerMapping's raise an exception
Returns: the resolved matcher, or null
/**
* Find the {@link HandlerMapping} that would handle the given request and
* return it as a {@link MatchableHandlerMapping} that can be used to test
* request-matching criteria.
* <p>If the matching HandlerMapping is not an instance of
* {@link MatchableHandlerMapping}, an IllegalStateException is raised.
* @param request the current request
* @return the resolved matcher, or {@code null}
* @throws Exception if any of the HandlerMapping's raise an exception
*/
@Nullable
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
Assert.notNull(this.pathPatternMatchableHandlerMappings, "Handler mappings with PathPatterns not initialized");
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
Object handler = handlerMapping.getHandler(wrapper);
if (handler == null) {
continue;
}
if (handlerMapping instanceof MatchableHandlerMapping) {
return this.pathPatternMatchableHandlerMappings.getOrDefault(
handlerMapping, (MatchableHandlerMapping) handlerMapping);
}
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
}
return null;
}
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
RequestAttributeChangeIgnoringWrapper wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
HandlerExecutionChain handler = null;
try {
handler = handlerMapping.getHandler(wrapper);
}
catch (Exception ex) {
// Ignore
}
if (handler == null) {
continue;
}
for (HandlerInterceptor interceptor : handler.getInterceptorList()) {
if (interceptor instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper);
}
}
if (handler.getHandler() instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper);
}
}
return null;
}
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
applicationContext, HandlerMapping.class, true, false);
if (!beans.isEmpty()) {
List<HandlerMapping> mappings = new ArrayList<>(beans.values());
AnnotationAwareOrderComparator.sort(mappings);
return Collections.unmodifiableList(mappings);
}
return Collections.unmodifiableList(initFallback(applicationContext));
}
private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) {
Properties props;
String path = "DispatcherServlet.properties";
try {
Resource resource = new ClassPathResource(path, DispatcherServlet.class);
props = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
}
String value = props.getProperty(HandlerMapping.class.getName());
String[] names = StringUtils.commaDelimitedListToStringArray(value);
List<HandlerMapping> result = new ArrayList<>(names.length);
for (String name : names) {
try {
Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
result.add((HandlerMapping) mapping);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
}
}
return result;
}
private static Map<HandlerMapping, MatchableHandlerMapping> initPathPatternMatchableHandlerMappings(
List<HandlerMapping> mappings) {
return mappings.stream()
.filter(mapping -> mapping instanceof MatchableHandlerMapping)
.map(mapping -> (MatchableHandlerMapping) mapping)
.filter(mapping -> mapping.getPatternParser() != null)
.collect(Collectors.toMap(mapping -> mapping, PathPatternMatchableHandlerMapping::new));
}
Request wrapper that ignores request attribute changes.
/**
* Request wrapper that ignores request attribute changes.
*/
private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
super(request);
}
@Override
public void setAttribute(String name, Object value) {
// Allow UrlPathHelper-resolved lookupPath to be saved for efficiency
if (name.equals(UrlPathHelper.PATH_ATTRIBUTE)) {
super.setAttribute(name, value);
}
}
}
}