基于Ant路径匹配规则AntPathMatcher的注意事项

目录
  • AntPathMatcher前言
  • 基本规则
  • 注意事项
  • 测试用例
  • spring url匹配工具类----AntPathMatcher
    • 具体使用场景
    • 请求body的二次写入

AntPathMatcher前言

(1)SpringMVC的路径匹配规则是依照Ant的来的,实际上不只是SpringMVC,整个Spring框架的路径解析都是按照Ant的风格来的;

(2)AntPathMatcher不仅可以匹配Spring的@RequestMapping路径,也可以用来匹配各种字符串,包括文件路径等。

基本规则

(1)? 匹配一个字符(除过操作系统默认的文件分隔符)

(2)* 匹配0个或多个字符

(3)**匹配0个或多个目录

(4){spring:[a-z]+} 将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量.

(PS:必须是完全匹配才行,在SpringMVC中只有完全匹配才会进入controller层的方法)

注意事项

(1)匹配文件路径,需要匹配某目录下及其各级子目录下所有的文件,使用/**/*而非*.*,因为有的文件不一定含有文件后缀;

(2)匹配文件路径,使用AntPathMatcher创建一个对象时,需要注意AntPathMatcher也有有参构造,传递路径分隔符参数pathSeparator,对于文件路径的匹配来说,则需要根据不同的操作系统来传递各自的文件分隔符,以此防止匹配文件路径错误。源码截图如下:

可以看到,AntPathMatcher默认路径分隔符为“/”,而在匹配文件路径时,需要注意Windows下路径分隔符为“\”,Linux下为“/”,写法即为:

AntPathMatcher matcher = new AntPathMatcher(File.separator);
AntPathMatcher matcher = new AntPathMatcher(System.getProperty("file.separator"));

(3)最长匹配规则(has more characters),即越精确的模式越会被优先匹配到。例如,URL请求/app/dir/file.jsp,现在存在两个路径匹配模式/**/*.jsp和/app/dir/*.jsp,那么会根据模式/app/dir/*.jsp来匹配。

测试用例

    // test exact matching
    assertTrue(pathMatcher.match("test", "test"));
    assertTrue(pathMatcher.match("/test", "/test"));
    assertTrue(pathMatcher.match("http://example.org", "http://example.org")); // SPR-14141
    assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));
    assertFalse(pathMatcher.match("test", "/test"));
    assertFalse(pathMatcher.match("/test", "test"));
    // test matching with ?'s
    assertTrue(pathMatcher.match("t?st", "test"));
    assertTrue(pathMatcher.match("??st", "test"));
    assertTrue(pathMatcher.match("tes?", "test"));
    assertTrue(pathMatcher.match("te??", "test"));
    assertTrue(pathMatcher.match("?es?", "test"));
    assertFalse(pathMatcher.match("tes?", "tes"));
    assertFalse(pathMatcher.match("tes?", "testt"));
    assertFalse(pathMatcher.match("tes?", "tsst"));
    // test matching with *'s
    assertTrue(pathMatcher.match("*", "test"));
    assertTrue(pathMatcher.match("test*", "test"));
    assertTrue(pathMatcher.match("test*", "testTest"));
    assertTrue(pathMatcher.match("test/*", "test/Test"));
    assertTrue(pathMatcher.match("test/*", "test/t"));
    assertTrue(pathMatcher.match("test/*", "test/"));
    assertTrue(pathMatcher.match("*test*", "AnothertestTest"));
    assertTrue(pathMatcher.match("*test", "Anothertest"));
    assertTrue(pathMatcher.match("*.*", "test."));
    assertTrue(pathMatcher.match("*.*", "test.test"));
    assertTrue(pathMatcher.match("*.*", "test.test.test"));
    assertTrue(pathMatcher.match("test*aaa", "testblaaaa"));
    assertFalse(pathMatcher.match("test*", "tst"));
    assertFalse(pathMatcher.match("test*", "tsttest"));
    assertFalse(pathMatcher.match("test*", "test/"));
    assertFalse(pathMatcher.match("test*", "test/t"));
    assertFalse(pathMatcher.match("test/*", "test"));
    assertFalse(pathMatcher.match("*test*", "tsttst"));
    assertFalse(pathMatcher.match("*test", "tsttst"));
    assertFalse(pathMatcher.match("*.*", "tsttst"));
    assertFalse(pathMatcher.match("test*aaa", "test"));
    assertFalse(pathMatcher.match("test*aaa", "testblaaab"));
    // test matching with ?'s and /'s
    assertTrue(pathMatcher.match("/?", "/a"));
    assertTrue(pathMatcher.match("/?/a", "/a/a"));
    assertTrue(pathMatcher.match("/a/?", "/a/b"));
    assertTrue(pathMatcher.match("/??/a", "/aa/a"));
    assertTrue(pathMatcher.match("/a/??", "/a/bb"));
    assertTrue(pathMatcher.match("/?", "/a"));
    // test matching with **'s
    assertTrue(pathMatcher.match("/**", "/testing/testing"));
    assertTrue(pathMatcher.match("/*/**", "/testing/testing"));
    assertTrue(pathMatcher.match("/**/*", "/testing/testing"));
    assertTrue(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla"));
    assertTrue(pathMatcher.match("/bla/**/bla", "/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/test", "/XXXbla/test"));
    assertFalse(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));
    assertFalse(pathMatcher.match("/*bla/test", "XXXblab/test"));
    assertFalse(pathMatcher.match("/*bla/test", "XXXbl/test"));
    assertFalse(pathMatcher.match("/????", "/bala/bla"));
    assertFalse(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));
    assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));
    assertTrue(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));
    assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));
    assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));
    assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/"));
    assertTrue(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing"));
    assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing"));
    assertFalse(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing"));
    assertFalse(pathMatcher.match("/x/x/**/bla", "/x/x/x/"));
    assertTrue(pathMatcher.match("/foo/bar/**", "/foo/bar")) ;
    assertTrue(pathMatcher.match("", ""));
    assertTrue(pathMatcher.match("/{bla}.*", "/testing.html"));

spring url匹配工具类----AntPathMatcher

在gateway进行授权认证时,有些请求url需要过滤掉,针对带/service/{id}/user-info这种带操作符的请求,需要特殊处理----AntPathMatcher就上场啦

具体使用场景

1.登录授权验证:过滤掉登录请求,一些资源获取请求

2.请求接口日志打印:过滤掉文件上传和下载的一些请求,requestBody里的文件流会被异常修改

具体代码:

请求body的二次写入

@Component
public class CachePostBodyFilter implements GlobalFilter, Ordered {
    private final List<HttpMessageReader<?>> messageReaders;
    public CachePostBodyFilter() {
        this.messageReaders = HandlerStrategies.withDefaults().messageReaders();
    }
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        if (FilterUrl.excludeUrls(new FilterUrl(request.getPath().toString(), request.getMethod()))) {
            return chain.filter(exchange);
        }
        if (Objects.equals(request.getMethod(), HttpMethod.POST)) {
            ServerRequest serverRequest = ServerRequest.create(exchange,
                    messageReaders);
            Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)
                    .flatMap(body -> {
                        exchange.getAttributes().put(RequestConstants.REQUEST_BODY, body);
                        return Mono.just(body);
                    });
            BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
            HttpHeaders headers = new HttpHeaders();
            headers.putAll(exchange.getRequest().getHeaders());
            // the new content type will be computed by bodyInserter
            // and then set in the request decorator
            headers.remove(HttpHeaders.CONTENT_LENGTH);
            CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
            return bodyInserter.insert(outputMessage, new BodyInserterContext()).
                    then(Mono.defer(() -> {
                        ServerHttpRequest decorator = decorate(exchange, headers,
                                outputMessage);
                        return chain.filter(exchange.mutate().request(decorator).build());
                    }));
        }
        return chain.filter(exchange);
    }
    ServerHttpRequestDecorator decorate(ServerWebExchange exchange, HttpHeaders headers,
            CachedBodyOutputMessage outputMessage) {
        return new ServerHttpRequestDecorator(exchange.getRequest()) {
            @Override
            public HttpHeaders getHeaders() {
                long contentLength = headers.getContentLength();
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.putAll(headers);
                if (contentLength > 0) {
                    httpHeaders.setContentLength(contentLength);
                } else {
                    httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
                }
                return httpHeaders;
            }
            @Override
            public Flux<DataBuffer> getBody() {
                return outputMessage.getBody();
            }
        };
    }
    @Override
    public int getOrder() {
        return -8;
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FilterUrl {
    private String url;
    private HttpMethod method;
    public static boolean excludeUrls(FilterUrl targetUrl) {
        List<FilterUrl> excludeUrls = Lists.newArrayList();
        excludeUrls.add(new FilterUrl("/api/v1/service/users", HttpMethod.POST));
        excludeUrls.add(new FilterUrl("/api/v1/service/terms/{termId}/export", HttpMethod.GET));
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        return excludeUrls.stream()
                .anyMatch(url -> antPathMatcher.match(url.getUrl(), targetUrl.getUrl()) && url.getMethod().equals(targetUrl.getMethod()));
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 基于Spring概念模型:PathMatcher 路径匹配器

    目录 概述 PathMatcher接口源代码 AntPathMatcher使用例子 spring的路径匹配工具 AntPathMatcher 以下代码为本人使用过的路径匹配工具代码 核心代码是这一行 源代码版本 : spring-webmvc-5.1.4.RELEASE 概述 PathMatcher是Spring的一个概念模型接口,该接口抽象建模了概念"路径匹配器",一个"路径匹配器"是一个用于路径匹配的工具.它的使用者是 : org.springframework

  • 详解Spring mvc ant path的使用方法

    详解Spring mvc ant path的使用方法 概要: 任何一个WEB都需要解决URL与请求处理器之间的映射,spring MVC也是一样,但Spring MVC就像Spring所作的一切一样(灵活,可以配置各种东西,但是也造成了很多复杂性),肯定不会只有一种方法来映射URL和 Controller之间的关系,并且在实际上,允许你自己创建映射规则和实现,而不仅仅依赖URL映射. 1.Spring path match Spring MVC中的路径匹配要比标准的web.xml要灵活的多.默认

  • 使用Spring AntPathMatcher的doMatch方法

    目录 AntPathMatcher的doMatch方法 有4个步骤 Spring的AntPathMatcher工具类用法 AntPathMatcher 下面是模糊匹配规则 AntPathMatcher的doMatch方法 AntPathMatcher.doMatch(...), 是解决模式匹配的源码 有4个步骤 1. 分解模式字符串, 分解路径字符串 2. 第一个while 循环, 用来判断绝对匹配 /xxx/abc ==> /xxx/abc 3. 第二个while循环两个字符串数组都从最后的下

  • spring mvc路径匹配原则详解

    在Spring MVC中经常要用到拦截器,在配置需要要拦截的路径时经常用到<mvc:mapping/>子标签,其有一个path属性,它就是用来指定需要拦截的路径的.例如: <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.i360r.platform.webapp.runtime.view.interceptor.GenericInterceptor"

  • 基于Ant路径匹配规则AntPathMatcher的注意事项

    目录 AntPathMatcher前言 基本规则 注意事项 测试用例 spring url匹配工具类----AntPathMatcher 具体使用场景 请求body的二次写入 AntPathMatcher前言 (1)SpringMVC的路径匹配规则是依照Ant的来的,实际上不只是SpringMVC,整个Spring框架的路径解析都是按照Ant的风格来的: (2)AntPathMatcher不仅可以匹配Spring的@RequestMapping路径,也可以用来匹配各种字符串,包括文件路径等. 基

  • Spring源码之请求路径匹配路由方式

    目录 请求路径匹配路由 入口 进入上面方法 SpringMVC 将请求找到匹配的处理 初始化映射关系 从映射关系中寻找匹配方法 请求路径匹配路由 在spring中,当一个请求过来的时候会做路径匹配,下面我们就从源码层面分析一下路径匹配. 示例: @RequestMapping(value = "/user/{aid}/online/**", method = RequestMethod.GET) 我们一起看看这个方法是如何寻找的,和一些相应的工具类 入口 我的项目使用的是自动配置的Re

  • SpringMVC路径匹配中使用通配符问题

    目录 SpringMVC路径匹配中使用通配符 @RequestMapping中指定的路径也可以使用通配符* 通配符不是只能放在最后的 通配符还可以匹配以某字符结束的路径 通配符还可以匹配以某字符开始的路径 同时存在路径变量和通配符匹配时的优先级关系 请求路径参数使用正则表达式 SpringMVC路径匹配中使用通配符 @RequestMapping中指定的路径也可以使用通配符* 表示任意字符.如下的处理器方法可以映射请求/antstyle/a,可以映射请求/antstyle/b,但是它不能映射请求

  • 详解Nginx location 匹配规则

    语法规则 location [=|~|~*|^~] /uri/ { - } 模式 含义 location = /uri = 表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前. location ~ pattern 开头表示区分大小写的正则匹配 location ~* pattern 开头表示不区分大小写的正则匹配 location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 location / 通用匹配

  • Nginx服务器的location指令匹配规则详解

    Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令.Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的"/uri/",可以是字符串或正则表达式.但如果要使用正则表达式,则必须指定前缀. nginx location语法 基本语法:location [=|~|~*|^~] /uri/ { - } = 严格匹配.如果这个查询匹配,那么将停止搜索并立即处理此请求. ~ 为区分大小写匹配(可用正则表达式) ~* 为不区分大小写

  • 基于express中路由规则及获取请求参数的方法

    express中常见的路由规则 主要使用的路由规则是get和post两种,即 var express = require('express'); var app = express(); app.get(); // get和post两种请求方式 app.post(); app.get()和app.post()的第一个参数为请求路径,第二个参数为处理请求的回调函数:回调函数有两个参数,分别为req和res,代表请求信息和响应信息. 获取请求路径和请求体中的各种参数 路径请求及对应获取请求路径的形式

  • servlet的url-pattern匹配规则详细描述(小结)

    一.概述 在利用servlet或Filter进行url请求的匹配时,很关键的一点就是匹配规则,但servlet容器中的匹配规则既不是简单的通配,也不是正则表达式,而是由自己的规则,比较容易混淆.本文来详细举例介绍下.下面的说明都是在tomcat服务器中得到验证的. 先介绍一下匹配的概念,上例子代码.在一个app(如名字为myapp)的web.xml文件中,有如下信息: <servlet> <servlet-name>MyServlet</servlet-name> &l

  • nginx 匹配规则小总结(推荐)

    nginx location 等号类型(=)的优先级最高,需要精确匹配.一旦匹配成功,则不再查找其他匹配项. ^~类型表达式.一旦匹配成功,则不再查找其他匹配项. 正则表达式类型(~ ~*)的优先级次之.如果有多个location的正则能匹配的话,则使用正则表达式最长的那个. (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/) ng

  • Nginx Location指令URI匹配规则详解小结

    1.介绍 location指令是http模块当中最核心的一项配置,根据预先定义的URL匹配规则来接收用户发送的请求,根据匹配结果,将请求转发到后台服务器.非法的请求直接拒绝并返回403.404.500错误处理等. 2.location指令语法 location [=|~|~*|^~|@] /uri/ { - } 或 location @name { - } 3.URI匹配模式 location指令分为两种匹配模式: 1> 普通字符串匹配:以=开头或开头无引导字符(-)的规则 2> 正则匹配:以

随机推荐