Spring AntPathMatcher 的使用

Spring AntPathMatcher语法:
The mapping matches URLs using the following rules:
? matches one character 问号匹配一个字符

  • matches zero or more characters *匹配多个字符
    ** matches zero or more directories in a path ** 匹配零个或多个目录,如果**后没有文件则不仅会匹配path的目录还会匹配文件
    {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring" 这里会将匹配到的文件或目录名以key:value的形式存储到map里.具体可以看代码

样例:
// com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp。 ?
// com/.jsp — matches all .jsp files in the com directory
// com//test.jsp — matches all test.jsp files underneath the com path
// org/springframework/
/
.jsp — matches all .jsp files underneath the org/springframework path
// org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
// com/{filename:\w+}.jsp will match com/test.jsp and assign the value test to the filename variable

测试代码 demo
` AntPathMatcher pathMatcher = new AntPathMatcher();
System.out.println(pathMatcher.match("/a/**","/a/b/a.jsp")); \true

     Map<String,String> map = pathMatcher.extractUriTemplateVariables("/a/{Spring:\\w+}.jsp","/a/yzw.jsp");
    System.out.println(map.get("Spring")); \\yzw

`


shiro 的 实现和 spring的实现功能有一点不同,shiro AntPathMatcher 不支持正则表达式模式。

REF:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

posted @ 2022-08-12 15:34  等一个人,咖啡  阅读(198)  评论(0编辑  收藏  举报