栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中的Ant路径匹配工具组件AntPathMatcher

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中的Ant路径匹配工具组件AntPathMatcher

Spring中的绝大多数的路径匹配规则是依照Ant的标准来的

实际上不只是SpringMVC,整个Spring框架的路径解析都是按照Ant的风格来的,在Spring中的具体实现,详情参见 org.springframework.util.AntPathMatcher,具体规则如下

a")
@ResponseBody
public String index(){
    return "index.html";
}
复制代码

结果:

index/a         true  输出 index.html(可以为0个目录)
index/x/a       true  输出 index.html(可以为一个目录)
index/x/z/c/a   true  输出 index.html(可以为多个目录)
复制代码

符号 {spring:[a-z]+}

其它的关于 AntPathMatcher 的文章里,对 {spring:[a-z]+} 的匹配大多是只字未提.这里补充下.

示例代码:

@RequestMapping("/index/{username:[a-b]+}") @ResponseBody public String index(@PathVariable("username") String username){ System.out.println(username); return username; }

结果:

index/ab true 输出 ab index/abbaaa true 输出 abbaaa index/a false 404错误 index/ac false 404错误

需求:我在做rbac权限校验的时候,设置管理员的访问路径为/admin.jsp和/app/dir/.jsp,那么会根据模式/app/dir**", "/testing/testing")); assertTrue(pathMatcher.match("*", "/testing/testing")); assertTrue(pathMatcher.match("/blabla", "/bla/testing/testing/bla")); assertTrue(pathMatcher.match("/blabla", "/bla/testing/testing/bla/bla")); assertTrue(pathMatcher.match("test", "/bla/bla/test")); assertTrue(pathMatcher.match("/bla**/bla", "/bla/bla/bla/bla/bla/bla")); assertTrue(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test")); assertTrue(pathMatcher.match("*bla", "/bla/bla/bla/bbb")); assertTrue(pathMatcher.match("**/bla**/bla**/bla**/bla**/bla**/bla**/bla**/blabla", "/x/x/x/")); assertTrue(pathMatcher.match("/foo/barexample 、/app/.x 类似于这样语法而负责真正判断是否匹配的工具类就是AntPathRequestMatcher

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号