美文网首页
Spring之AntPathMatcher

Spring之AntPathMatcher

作者: 逆水寻洲 | 来源:发表于2019-08-05 19:56 被阅读0次

    在使用Zuul 做网关权限控制的时候,需要判断当前登陆用户是否有权限访问接口。这里就涉及到url路径匹配问题。这里因为用户权限开发过程中使用到了这方面的知识,所以系统学习一下。

    AntPathMatcher是路径匹配规则工具类,可以根据ant路径匹配规则判断给定的字符串是否匹配。

    ant 匹配规则如下:
    1. ? 匹配一个字符(除过操作系统默认的文件分隔符)
    2. * 匹配0个或多个字符
    3. **匹配0个或多个目录
    用例如下
    image.png

    测试用例:

    public class AntPathMatcherTest {
    
        public static void main(String[] args) {
            PathMatcher pathMatcher = new AntPathMatcher();
            System.out.println(pathMatcher.match("/*/retail/**", "/cir-node/retail/save"));     //true
            System.out.println(pathMatcher.match("*test*", "AnothertestTest"));     //true
            System.out.println(pathMatcher.match("/????", "/bala/bla"));            //false
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring之AntPathMatcher

          本文链接:https://www.haomeiwen.com/subject/gmumdctx.html