Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MappedInterceptor in 5.3 does not support all AntPatternMatcher patterns  #26690

@protobufel2

Description

@protobufel2

Spring Framework 5.3 (up to 5.3.5) MappedInterceptor's constructor effectively prevents the use of any PathMatcher, including legacy AntPathMatcher, making it completely incompatible with all earlier versions. Here is why:

  1. The InterceptorRegistration#getInterceptor tries to create MappedInterceptor, however it'll fail, due to 2.
	protected Object getInterceptor() {

		if (this.includePatterns == null && this.excludePatterns == null) {
			return this.interceptor;
		}

		MappedInterceptor mappedInterceptor = new MappedInterceptor(
				StringUtils.toStringArray(this.includePatterns),
				StringUtils.toStringArray(this.excludePatterns),
				this.interceptor);

		if (this.pathMatcher != null) {
			mappedInterceptor.setPathMatcher(this.pathMatcher);
		}

		return mappedInterceptor;
	}
  1. MappedInterceptor's base constructor calls MappedInterceptor#initPatterns, which would use the default PathPatternParser and will fail for the Ant-like patterns and suffixes (old style) no matter whether set in PathMatchConfigurer or not:
	public MappedInterceptor(@Nullable String[] includePatterns, @Nullable String[] excludePatterns,
			HandlerInterceptor interceptor, @Nullable PathPatternParser parser) {

		this.includePatterns = initPatterns(includePatterns, parser);
		this.excludePatterns = initPatterns(excludePatterns, parser);
		this.interceptor = interceptor;
	}

	@Nullable
	private static PathPattern[] initPatterns(
			@Nullable String[] patterns, @Nullable PathPatternParser parser) {

		if (ObjectUtils.isEmpty(patterns)) {
			return null;
		}
		parser = (parser != null ? parser : PathPatternParser.defaultInstance);
		return Arrays.stream(patterns).map(parser::parse).toArray(PathPattern[]::new);
	}

The problem is that include/excludePatterns type have changed from String[] to PathPattern[]. As a possible solution to this could be having a chameleon PathPattern, i.e. NOOP PathPattern holding just the String underneath and having isNoop() to differentiate from the real ones, to be used in concert with the MappedInterceptor constructor with a PathMatcher parameter, and the rest of the MappedInterceptor code checking for isNoop() accordingly.
In addition, MappedInterceptor(@Nullable String[] includePatterns, @Nullable String[] excludePatterns, HandlerInterceptor interceptor) can be using such NOOP PathPattern and be compatible with InterceptorRegistration#getInterceptor and friends.

Unless there is a simple and obvious solution/config already, this is a major, showstopper bug.

Thanks much in advance,
David

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions