美文网首页
AntPathMatcher匹配规则测试

AntPathMatcher匹配规则测试

作者: 伊夫_艾尔斯 | 来源:发表于2024-06-19 17:11 被阅读0次

项目里面HttpSecurity需要配置antMatchers的路径匹配规则,看内部使用的是AntPathMatcher,做了简单的测试比对, 对比了1,2,3级路径,单与双.

  • 测试代码:

    public static void main(String[] args) {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match01 = pathMatcher.match("/monitor/*",    "/monitor");
        boolean match02 = pathMatcher.match("/monitor/**",   "/monitor");
        boolean match03 = pathMatcher.match("/monitor/*/**", "/monitor");
        boolean match04 = pathMatcher.match("/mon*",         "/monitor");
        boolean match05 = pathMatcher.match("/mon*or",       "/monitor");
        boolean match06 = pathMatcher.match("/mon**",        "/monitor");
        boolean match07 = pathMatcher.match("/mon*/*",       "/monitor");
        boolean match08 = pathMatcher.match("/mon*/**",      "/monitor");

        System.out.println("------------------------------------------------------------------------------------------------------------");

        System.out.println("match01:"+ match01);
        System.out.println("match02:"+ match02);
        System.out.println("match03:"+ match03);
        System.out.println("match04:"+ match04);
        System.out.println("match05:"+ match05);
        System.out.println("match06:"+ match06);
        System.out.println("match07:"+ match07);
        System.out.println("match08:"+ match08);

        boolean match11 = pathMatcher.match("/monitor/*",    "/monitor/server");
        boolean match12 = pathMatcher.match("/monitor/**",   "/monitor/server");
        boolean match13 = pathMatcher.match("/monitor/*/**", "/monitor/server");
        boolean match14 = pathMatcher.match("/mon*",         "/monitor/server");
        boolean match15 = pathMatcher.match("/mon*or",       "/monitor/server");
        boolean match16 = pathMatcher.match("/mon**",        "/monitor/server");
        boolean match17 = pathMatcher.match("/mon*/*",       "/monitor/server");
        boolean match18 = pathMatcher.match("/mon*/**",      "/monitor/server");

        System.out.println("------------------------------------------------------------------------------------------------------------");

        System.out.println("match11:"+ match11);
        System.out.println("match12:"+ match12);
        System.out.println("match13:"+ match13);
        System.out.println("match14:"+ match14);
        System.out.println("match15:"+ match15);
        System.out.println("match16:"+ match16);
        System.out.println("match17:"+ match17);
        System.out.println("match18:"+ match18);

        boolean match21 = pathMatcher.match("/monitor/*",    "/monitor/server/detail");
        boolean match22 = pathMatcher.match("/monitor/**",   "/monitor/server/detail");
        boolean match23 = pathMatcher.match("/monitor/*/**", "/monitor/server/detail");
        boolean match24 = pathMatcher.match("/mon*",         "/monitor/server/detail");
        boolean match25 = pathMatcher.match("/mon*or",       "/monitor/server/detail");
        boolean match26 = pathMatcher.match("/mon**",        "/monitor/server/detail");
        boolean match27 = pathMatcher.match("/mon*/*",       "/monitor/server/detail");
        boolean match28 = pathMatcher.match("/mon*/**",      "/monitor/server/detail");

        System.out.println("------------------------------------------------------------------------------------------------------------");

        System.out.println("match21:"+ match21);
        System.out.println("match22:"+ match22);
        System.out.println("match23:"+ match23);
        System.out.println("match24:"+ match24);
        System.out.println("match25:"+ match25);
        System.out.println("match26:"+ match26);
        System.out.println("match27:"+ match27);
        System.out.println("match28:"+ match28);

        System.out.println("------------------------------------------------------------------------------------------------------------");
    }

}


  • 输出结果:
------------------------------------------------------------------------------------------------------------
match01:false
match02:true
match03:false
match04:true
match05:true
match06:true
match07:false
match08:true
------------------------------------------------------------------------------------------------------------
match11:true
match12:true
match13:true
match14:false
match15:false
match16:false
match17:true
match18:true
------------------------------------------------------------------------------------------------------------
match21:false
match22:true
match23:true
match24:false
match25:false
match26:false
match27:false
match28:true
------------------------------------------------------------------------------------------------------------


简单总结: *是通配符,匹配一级路径中的任意字符; **单独使用和* 一样,配合/即/**则是多级任意路径(可以没有该级)

相关文章

  • Spring之AntPathMatcher

    前言 AntPathMatcher是什么?主要用来解决什么问题? 背景:在做uri匹配规则发现这个类,根据spri...

  • springboot 集成Swagger2报错解决

    Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是...

  • 一条日志正则的匹配

    匹配规则 1 匹配规则 2 匹配规则 3 匹配规则 4

  • Nginx中的location规则补充

    这是一篇关于Nginx中Location规则的补充说明。重点在于测试路径规则和^~规则以及正则规则同时匹配的情况,...

  • IPTable简介3——常用匹配

    三 、IPtables的常用匹配规则 匹配规则可以用!号进行非运算 1、常规匹配 1.1 通用匹配规则 -s(--...

  • netfilter之match和target

    匹配规则 iptables的rule中,匹配规则包含标准匹配和扩展匹配。 标准匹配标准匹配使用如下结构体表示,包含...

  • nginx 配置简介

    location 匹配规则 语法规则 location [=||*|^~] /uri/ { … } 前缀匹配时,N...

  • 1.shell变量替换

    1.${变量#匹配规则} 从头开始,最短匹配 2.${变量##匹配规则} 从头开始,最长匹配 3.${变量%匹...

  • 匹配规则

    1 最先开始的匹配拥有最高优先权2 贪婪匹配当正则表达式中能接受重复的限定符时,通常的行为是,在使整个表达式得到匹...

  • Hiho 1289 403 Forbidden(微软编程题)

    题意 输入 ip 按顺序匹配规则,优先匹配前面的规则,如果没有规则可以匹配则视为合法。注意:掩码为 0 的时候表示...

网友评论

      本文标题:AntPathMatcher匹配规则测试

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