Spring路径匹配器AntPathMatcher


PathMatcher接口

Spring的PathMatcher路径匹配器接口,用于支持带通配符的资源路径匹配。

使用场景

PathMatcher接口在Spring的许多场景下使用,比如:

  • PathMatchingResourcePatternResolver:资源扫描,启动时扫描并加载资源
  • AbstractUrlHandlerMapping:请求路径映射到 Controller
  • WebContentInterceptor:拦截器拦截路径分析

接口方法

方法 描述
boolean isPattern(String path) 判断路径是否是模式
boolean match(String pattern, String path) 判断路径是否完全匹配
boolean matchStart(String pattern, String path) 判断路径是否前缀匹配
前缀匹配的意思:路径能与模式的前面部分匹配,但模式可能还有后面多余部分
例如:/test能前缀匹配/test/{id}(但模式还有多余的/{id}部分未匹配)
String extractPathWithinPattern(String pattern, String path) 得到模式匹配的部分值
该方法只返回路径的实际模式匹配部分
例如:myroot/*.html 匹配 myroot/myfile.html 路径,结果为 myfile.html
Map<String, String> extractUriTemplateVariables(String pattern, String path) 提取路径中的路径参数值
Comparator<String> getPatternComparator(String path) 得到一个排序比较器,用于对匹配到的所有路径进行排序
String combine(String pattern1, String pattern2) 合并两个模式

AntPathMatcher类

AntPathMatcher是Spring为PathMatcher接口提供的默认实现,支持Ant风格的路径匹配。

匹配规则

AntPathMatcher支持的匹配规则:

规则 描述
? 匹配一个字符
* 在一个路径段中匹配零个、一个或多个字符
** 匹配零个或多个路径段,直到路径结束
{id} 匹配一个路径段,并将该路径段的值作为变量id的变量值
{id:[a-z]+} 匹配一个满足正则([a-z]+)路径段,并将该路径段的值作为变量id的变量值

举例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        // ?
        System.out.println(matcher.match("/test/a?c", "/test/abc"));// true
        // *
        System.out.println(matcher.match("/test/*", "/test/"));// true
        System.out.println(matcher.match("/test/*", "/test/aa"));// true
        System.out.println(matcher.match("/test/*.html", "/test/aa"));// false
        // **
        System.out.println(matcher.match("/test/**", "/test/"));// true
        System.out.println(matcher.match("/test/**", "/test/aa"));// true
        System.out.println(matcher.match("/test/**", "/test/aa/bb"));// true
        // {id}
        System.out.println(matcher.match("/test/{id}", "/test/111"));// true
        System.out.println(matcher.match("/test/a{id}", "/test/a111"));// true
        System.out.println(matcher.match("/test/{id}/aa", "/test/111/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-haha/aa"));// true
        // {id:[a-z]+}
        System.out.println(matcher.match("/test/{id:[a-z]+}", "/test/111"));// false
        System.out.println(matcher.match("/test/{id:[a-z]+}", "/test/abc"));// true
        System.out.println(matcher.match("/test/{id:\\w+}", "/test/1a_"));// true
        System.out.println(matcher.match("/test/{id:\\w+}", "/test/--"));// false
    }

一些不匹配情况原因:

模式 路径 原因
/user/aaa/ /user/aaa 结束符不一致
/user/*/ /user/aaa 结束符不一致

实际在Spring项目中使用的时候,你会发现:就算实际请求的结束符为/,但还是能匹配成功。这又是为什么呢?

两个关键属性:

  • useSuffixPatternMatch:设置是否使用后缀模式匹配,如“/user”是否匹配/user.*,默认是

    这种模式下,实际请求.后面加任何后缀,都会匹配到。如:实际请求“/user.html”能匹配上“/user”。

  • useTrailingSlashMatch:设置是否使用后缀路径模式匹配,如“/user”是否匹配“/user/”,默认是

    这种模式下,实际请求加、后缀,都会匹配到。如:实际请求“/user/”能匹配上“/user”。

如何修改useSuffixPatternMatch、useTrailingSlashMatch属性的值,可参考文章:https://blog.csdn.net/m0_56069948/article/details/124791784

关键源码:

PatternsRequestCondition类的getMatchingPattern方法

	private String getMatchingPattern(String pattern, String lookupPath) {
    
    
		// 模式与路径相等,直接返回模式
		if (pattern.equals(lookupPath)) {
    
    
			return pattern;
		}
		// 如果使用后缀模式匹配,返回的模式会拼接上合适的后缀,如.html
		if (this.useSuffixPatternMatch) {
    
    
			if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {
    
    
				for (String extension : this.fileExtensions) {
    
    
					if (this.pathMatcher.match(pattern + extension, lookupPath)) {
    
    
						return pattern + extension;
					}
				}
			}
			else {
    
    
				boolean hasSuffix = pattern.indexOf('.') != -1;
				if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
    
    
					return pattern + ".*";
				}
			}
		}
		if (this.pathMatcher.match(pattern, lookupPath)) {
    
    
			return pattern;
		}
		// 如果使用后缀路径模式匹配,返回的模式会拼接上/
		if (this.useTrailingSlashMatch) {
    
    
			if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
    
    
				return pattern + "/";
			}
		}
		return null;
	}

因此,getMatchingPattern方法返回的模式再与请求路径进行模式匹配当然能匹配上了。

主要方法

1. isPattern

判断路径是否是模式。

只要路径中拥有 *?{},则就是模式。

public boolean isPattern(@Nullable String path) {
    
    
    if (path == null) {
    
    
        return false;
    }
    boolean uriVar = false;
    for (int i = 0; i < path.length(); i++) {
    
    
        char c = path.charAt(i);
        if (c == '*' || c == '?') {
    
    
            return true;
        }
        if (c == '{') {
    
    
            uriVar = true;
            continue;
        }
        if (c == '}' && uriVar) {
    
    
            return true;
        }
    }
    return false;
}

示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.isPattern("/test/{id}"));// true
    }

2. match

判断路径是否完全匹配

public boolean match(String pattern, String path) {
    
    
    return doMatch(pattern, path, true, null);
}

示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.match("/test/*", "/test/111"));// true
        System.out.println(matcher.match("/test/**", "/test/111/222"));// true
        System.out.println(matcher.match("/test/{id}", "/test/111"));// true
        System.out.println(matcher.match("/test/{id}/aa", "/test/111/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-haha/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-/aa"));// true
    }

3. matchStart

判断路径是否前缀匹配

前缀匹配的意思:路径能与模式的前面部分匹配,但模式可能还有后面多余部分(可以理解为模式是否是以路径开头)

public boolean matchStart(String pattern, String path) {
    
    
    return doMatch(pattern, path, false, null);
}

示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.matchStart("/test/*", "/test"));// true
        System.out.println(matcher.matchStart("/test/aa/*", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}-{name}/aa", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}", "/test/111/222"));// false
    }

4. extractPathWithinPattern

得到模式匹配的映射部分。找出通过*或者?匹配上的那一段路径及其后续路径。

示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test"));//
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test/aa"));// aa
        System.out.println(matcher.extractPathWithinPattern("/test/**", "/test/aa/bb"));// aa/bb
        System.out.println(matcher.extractPathWithinPattern("/test/a?c/aa", "/test/abc/aa"));// abc/aa
        System.out.println(matcher.extractPathWithinPattern("/test/aa?c/aa/cc", "/test/abc/aa"));// abc/aa
    }

5. extractUriTemplateVariables

路径必须完全匹配(否则抛出异常),并提取路径中的路径参数值。

示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.extractUriTemplateVariables("/test/{id}", "/test/111"));// {id=111}
        System.out.println(matcher.extractUriTemplateVariables("/test/a{id}", "/test/a111"));// {id=111}
        System.out.println(matcher.extractUriTemplateVariables("/test/{id}/aa", "/test/111/aa"));// {id=111}
        System.out.println(matcher.extractUriTemplateVariables("/test/{id}-{name}/aa", "/test/111-haha/aa"));// {id=111, name=haha}
        System.out.println(matcher.extractUriTemplateVariables("/test/{id:[a-z]+}", "/test/abc"));// {id=abc}
        System.out.println(matcher.extractUriTemplateVariables("/test/{id:\\w+}", "/test/1a_"));// {id=1a_}
    }

6. getPatternComparator

得到一个排序比较器。

	public Comparator<String> getPatternComparator(String path) {
    
    
		return new AntPatternComparator(path);
	}

7. combine

合并两个模式。
示例:

    public static void main(String[] args) {
    
    
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.combine("/test/*", "/test/aa"));// /test/aa
        System.out.println(matcher.combine("/test/*", "/test/aa/bb"));// /test/test/aa/bb
        System.out.println(matcher.combine("/test/**", "/test/aa"));// /test/aa
        System.out.println(matcher.combine("/test/{id}", "/test/aa"));// /test/{id}/test/aa
    }

参考文章:
https://blog.csdn.net/gongm24/article/details/124961800

猜你喜欢

转载自blog.csdn.net/JokerLJG/article/details/127751174