Throw exception for illegal PathMatch configurations
With the new `ParsingPathMatcher` implementation, new patterns are now
allowed, such as `"/foo/{*bar}". The `"{*bar}"` segment will capture
everything until the end of the given path. Adding other elements after
that segment is illegal and will throw exceptions.
One can configure on a `PathMatchConfigurer` various options like
`useTrailingSlashMatch` and `useSuffixPatternMatch`; those options, when
enabled, will try to append suffixes like `".*"` and `"/"` to existing
path patterns. In case of a "capture the rest" pattern segment, those
options won't be honored.
This is why this commit ensures that an exception is thrown at the start
of the application if an illegal configuration is applied to the
`PathMatchConfigurer`.
Issue: SPR-15303, SPR-15558
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
|
||||
/**
|
||||
* Helps with configuring HandlerMappings path matching options such as trailing
|
||||
@@ -123,6 +124,12 @@ public class PathMatchConfigurer {
|
||||
}
|
||||
|
||||
public PathMatcher getPathMatcher() {
|
||||
if(this.pathMatcher != null
|
||||
&& this.pathMatcher.getClass().isAssignableFrom(ParsingPathMatcher.class)
|
||||
&& (this.trailingSlashMatch || this.suffixPatternMatch)) {
|
||||
throw new IllegalStateException("When using a ParsingPathMatcher, useTrailingSlashMatch" +
|
||||
" and useSuffixPatternMatch should be set to 'false'.");
|
||||
}
|
||||
return this.pathMatcher;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,14 @@ public interface WebMvcConfigurer {
|
||||
* <li>ViewControllerMappings</li>
|
||||
* <li>ResourcesMappings</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Note that if a {@link org.springframework.web.util.pattern.ParsingPathMatcher}
|
||||
* is configured here,
|
||||
* the {@link PathMatchConfigurer#setUseTrailingSlashMatch(Boolean)} and
|
||||
* {@link PathMatchConfigurer#setUseSuffixPatternMatch(Boolean)} options must be set
|
||||
* to {@literal false}as they can lead to illegal patterns,
|
||||
* see {@link org.springframework.web.util.pattern.ParsingPathMatcher}.
|
||||
*
|
||||
* @since 4.0.3
|
||||
*/
|
||||
default void configurePathMatch(PathMatchConfigurer configurer) {
|
||||
|
||||
Reference in New Issue
Block a user