Spring路径匹配器AntPathMatcher

这篇具有很好参考价值的文章主要介绍了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文章来源地址https://www.toymoban.com/news/detail-560421.html

到了这里,关于Spring路径匹配器AntPathMatcher的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 持续集成部署-k8s-服务发现-Ingress 路径匹配与虚拟主机匹配

    要使用 Ingress-Nginx 首先第一步是需要先安装它,安装的步骤可以参考:持续集成部署-k8s-服务发现-Ingress 这里我使用之前创建的 Service:持续集成部署-k8s-服务发现-Service 我这里创建的 Service 的名称是: nginx-svc 这个后面要用到。

    2024年02月07日
    浏览(16)
  • Nginx精确匹配并跳转到指定路径

    Nginx精确匹配并跳转到指定路径

    说明: 1、根据研发要求,将/welcome页面跳转到指定页面/example 2、请在Nginx配置文件server中配置 3、请查阅如下图所示:https://xxxx/welcom ——https://xxxx/example 说明:将所有精确匹配到/welcom的请求都重定向到/example这个路径下,并且以永久的方式进行重定向。

    2024年02月12日
    浏览(10)
  • error: 路径规格 ‘xxxxxxxxx‘ 未匹配任何git已知文件

    error: 路径规格 ‘xxxxxxxxx‘ 未匹配任何git已知文件

    git学习笔记之git工作流程 - 掘金 针对git切换分支时,可能出现的两种情况:

    2024年02月11日
    浏览(24)
  • 【Android Gradle 插件】Android 依赖管理 ① ( 依赖库匹配 | 依赖库查找顺序及路径 | Gradle 资源库 )

    【Android Gradle 插件】Android 依赖管理 ① ( 依赖库匹配 | 依赖库查找顺序及路径 | Gradle 资源库 )

    依赖库匹配 :  依赖库由三部分组成 依赖库分组 依赖库名称 依赖库版本号 只有三者都对上 , 依赖库才能匹配上 , 如 依赖库分组为 androidx.appcompat ,  依赖库名称为 appcompat ,  依赖库版本号为 1.3.1 ,  三者由冒号隔开 ; Android 依赖库查找路径 : 首先 , 查找 本地的 Gradle 缓存依赖

    2024年01月17日
    浏览(15)
  • Spring Security:PasswordEncoder密码加密匹配操作

    Spring Security:PasswordEncoder密码加密匹配操作

    目录 PasswordEncoder SpringBoot:注入BSryptPasswordEncoder实例 BSryptPasswordEncoder详解 父接口PasswordEncoder BSryptPasswordEncoder及其使用 成员方法 SecurityUtils安全服务工具类 测试代码         PasswordEncoder是Spring Security框架默认使用的密码加密器,对应的数据表 sys_user 的密码 password 字段需

    2023年04月26日
    浏览(10)
  • 【源码】Spring Cloud Gateway 是在哪里匹配路由的?

    【源码】Spring Cloud Gateway 是在哪里匹配路由的?

    我们知道,经过网关的业务请求会被路由到后端真实的业务服务上去,假如我们使用的是Spring Cloud Gateway,那么你知道Spring Cloud Gateway是在哪一步去匹配路由的吗? 源码之下无秘密,让我们一起从源码中寻找答案。 Spring Cloud Gateway 的入口为 DispatcherHandler 的 handle 方法,其中主

    2023年04月24日
    浏览(11)
  • Spring-cloud-gateway 路由配置方式及匹配规则

    1.1 基础路由配置⽅式 如果请求的⽬标地址,是单个的URI资源路径,配置⽂件实例如下: 各字段含义如下。 id:我们⾃定义的路由 ID,保持唯⼀ uri:⽬标服务地址 predicates:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate

    2024年02月04日
    浏览(13)
  • spring-cloud-gateway版本和springboot版本不匹配

    在搭建gateway服务的时候,启动出现以下问题: Description: An attempt was made to call a method that does not exist. The attempt was made from the following location:     org.springframework.cloud.gateway.config.GatewayAutoConfiguration$NettyConfiguration.buildConnectionProvider(GatewayAutoConfiguration.java:798) The following method did no

    2024年02月16日
    浏览(13)
  • Spring Boot 配置静态资源路径

    在Spring Boot 2.7.2版本中,查看默认静态资源路径,在 WebProperties.class 中如下 可以看到默认资源路径有4个。 使用 Spring Initializr 新建Spring Boot项目,自带 static 目录,直接将前端资源文件放到该目录下,启动项目,访问 http://localhost:端口号/资源目录/名称.html 即可; 例如,有一个

    2023年04月15日
    浏览(11)
  • 【Spring类路径Bean定义信息扫描】

    补充一下: AbstractBeanDefinition、AnnotatedBeanDefinition都是spring框架中定义和处理BeanDefinition的类,他们在SpringIOC容器的核心机制中有重要的地位。 1. AbstractBeanDefinition: 是个抽象类,实现了 BeanDefinition 接口,为BeanDefinition提供了一些通用的方法和属性。 提供了BeanDefinition的基本结构

    2024年01月16日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包