版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cainiao111112/article/details/82885611
作用:在项目中主要用来做路径的匹配,在权限模块会用到接口路径的匹配。
用法规则:
?匹配一个字符
* 匹配零个或多个字符
** 匹配路径中零个或多个目录
例子:
com / t?st.jsp ----- 匹配com / test.jsp,还有com / tast.jsp或com / txst.jsp
com / * .jsp ------ 匹配com目录中的所有.jsp文件,com/abc.jsp,com/def.jsp
com / ** / test.jsp - 匹配com路径下的所有test.jsp文件,com/a/b/c/test.jsp,com/a/b/test.jsp
注意:模式和路径必须都是绝对的,或者必须都是相对的,以便两者匹配。因此,建议此实现的用户采取消毒模式,以便在它们使用的上下文中使用“/”作为前缀。
测试:
@Test
public void test() {
AntPathMatcher pathMatcher = new AntPathMatcher();
//?
assertTrue(pathMatcher.match("c?m", "com"));
assertTrue(pathMatcher.match("c?m", "cam"));
//*
assertTrue(pathMatcher.match("*", "abc"));
assertTrue(pathMatcher.match("*", "def"));
//**
assertTrue(pathMatcher.match("/*/**", "/a/b/c"));
assertTrue(pathMatcher.match("/**/*", "/a/b/c"));
}