最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項

 更新時間:2021年11月24日 15:58:05   作者:豆角茄子子  
這篇文章主要介紹了基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

AntPathMatcher前言

(1)SpringMVC的路徑匹配規(guī)則是依照Ant的來的,實際上不只是SpringMVC,整個Spring框架的路徑解析都是按照Ant的風格來的;

(2)AntPathMatcher不僅可以匹配Spring的@RequestMapping路徑,也可以用來匹配各種字符串,包括文件路徑等。

基本規(guī)則

(1)? 匹配一個字符(除過操作系統(tǒng)默認的文件分隔符)

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

(3)**匹配0個或多個目錄

(4){spring:[a-z]+} 將正則表達式[a-z]+匹配到的值,賦值給名為 spring 的路徑變量.

(PS:必須是完全匹配才行,在SpringMVC中只有完全匹配才會進入controller層的方法)

注意事項

(1)匹配文件路徑,需要匹配某目錄下及其各級子目錄下所有的文件,使用/**/*而非*.*,因為有的文件不一定含有文件后綴;

(2)匹配文件路徑,使用AntPathMatcher創(chuàng)建一個對象時,需要注意AntPathMatcher也有有參構(gòu)造,傳遞路徑分隔符參數(shù)pathSeparator,對于文件路徑的匹配來說,則需要根據(jù)不同的操作系統(tǒng)來傳遞各自的文件分隔符,以此防止匹配文件路徑錯誤。源碼截圖如下:

可以看到,AntPathMatcher默認路徑分隔符為“/”,而在匹配文件路徑時,需要注意Windows下路徑分隔符為“\”,Linux下為“/”,寫法即為:

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

(3)最長匹配規(guī)則(has more characters),即越精確的模式越會被優(yōu)先匹配到。例如,URL請求/app/dir/file.jsp,現(xiàn)在存在兩個路徑匹配模式/**/*.jsp和/app/dir/*.jsp,那么會根據(jù)模式/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進行授權(quán)認證時,有些請求url需要過濾掉,針對帶/service/{id}/user-info這種帶操作符的請求,需要特殊處理----AntPathMatcher就上場啦

具體使用場景

1.登錄授權(quán)驗證:過濾掉登錄請求,一些資源獲取請求

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()));
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 你知道Java判斷字符串是否為數(shù)字的多種方式嗎

    你知道Java判斷字符串是否為數(shù)字的多種方式嗎

    在編程的時候經(jīng)常遇到要判斷一個字符串中的字符是否是數(shù)字(0-9),所以下面這篇文章主要給大家介紹了關(guān)于Java判斷字符串是否為數(shù)字的多種方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • MyBatis解決Update動態(tài)SQL逗號的問題

    MyBatis解決Update動態(tài)SQL逗號的問題

    這篇文章主要介紹了MyBatis解決Update動態(tài)SQL逗號的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 使用Mybatis如何實現(xiàn)多個控制條件查詢

    使用Mybatis如何實現(xiàn)多個控制條件查詢

    這篇文章主要介紹了使用Mybatis如何實現(xiàn)多個控制條件查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java實現(xiàn)單鏈表中的增刪改

    java實現(xiàn)單鏈表中的增刪改

    這篇文章主要為大家詳細介紹了java實現(xiàn)單鏈表中的增刪改,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 源碼分析Java中ThreadPoolExecutor的底層原理

    源碼分析Java中ThreadPoolExecutor的底層原理

    這篇文章主要帶大家從源碼分析一下Java中ThreadPoolExecutor的底層原理,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下
    2023-05-05
  • mybatis判斷l(xiāng)ist不為空/大小的問題

    mybatis判斷l(xiāng)ist不為空/大小的問題

    這篇文章主要介紹了mybatis判斷l(xiāng)ist不為空/大小的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解Java 線程中斷

    詳解Java 線程中斷

    這篇文章主要介紹了Java 線程中斷的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Arrays.sort如何實現(xiàn)降序排序

    Arrays.sort如何實現(xiàn)降序排序

    這篇文章主要介紹了Arrays.sort如何實現(xiàn)降序排序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析

    Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析

    這篇文章主要介紹了Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Java LinkedList的實現(xiàn)原理圖文詳解

    Java LinkedList的實現(xiàn)原理圖文詳解

    今天小編就為大家分享一篇關(guān)于Java LinkedList的實現(xiàn)原理圖文詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01

最新評論

景宁| 红安县| 扶余县| 荔波县| 灵台县| 东城区| 西乌| 将乐县| 沈阳市| 池州市| 乐昌市| 临泽县| 靖西县| 长武县| 延津县| 杭锦后旗| 宝清县| 东乌| 通城县| 横峰县| 轮台县| 克拉玛依市| 万山特区| 上蔡县| 五大连池市| 茌平县| 湘潭市| 德惠市| 寿光市| 达拉特旗| 名山县| 阜新市| 双峰县| 仙居县| 西昌市| 湾仔区| 五大连池市| 岑溪市| 桐乡市| 大化| 滦南县|