1
0
mirror of synced 2026-08-04 17:27:13 +00:00

MvcRequestMatcher servletPath Polish / XML Config

Fixes gh-4014
This commit is contained in:
Joe Grandja
2016-07-22 10:24:12 -04:00
committed by Rob Winch
parent 8a6d0cd16d
commit dabcc5416a
10 changed files with 229 additions and 104 deletions
@@ -16,17 +16,24 @@
package org.springframework.security.web.servlet.util.matcher;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.util.matcher.RequestVariablesExtractor;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.ClassUtils;
import org.springframework.util.PathMatcher;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
import org.springframework.web.servlet.handler.RequestMatchResult;
import org.springframework.web.util.UrlPathHelper;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@@ -45,13 +52,18 @@ import java.util.Map;
* @since 4.1.1
*/
public class MvcRequestMatcher
implements RequestMatcher, RequestVariablesExtractor {
implements RequestMatcher, RequestVariablesExtractor, InitializingBean, ServletContextAware {
private static final boolean isServlet30 = ClassUtils.isPresent(
"javax.servlet.ServletRegistration", MvcRequestMatcher.class.getClassLoader());
private final DefaultMatcher defaultMatcher = new DefaultMatcher();
private final HandlerMappingIntrospector introspector;
private final String pattern;
private HttpMethod method;
private String servletPath;
private ServletContext servletContext;
public MvcRequestMatcher(HandlerMappingIntrospector introspector, String pattern) {
this.introspector = introspector;
@@ -100,6 +112,40 @@ public class MvcRequestMatcher
: result.extractUriTemplateVariables();
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
// servletPath is required when at least one registered Servlet
// is mapped to a path other than the default (root) path '/'
if (this.servletContext == null || !isServlet30) {
return;
}
if (this.getServletPath() != null) {
return;
}
for (ServletRegistration registration : this.servletContext.getServletRegistrations().values()) {
for (String mapping : registration.getMappings()) {
if (mapping.startsWith("/") && mapping.length() > 1) {
throw new IllegalStateException(
"servletPath must not be null for mvcPattern \"" + this.getMvcPattern()
+ "\" when providing a servlet mapping of "
+ mapping + " for servlet "
+ registration.getClassName());
}
}
}
}
/* (non-Javadoc)
* @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
*/
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
* @param method the method to set
*/