基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng)
AntPathMatcher前言
(1)SpringMVC的路徑匹配規(guī)則是依照Ant的來(lái)的,實(shí)際上不只是SpringMVC,整個(gè)Spring框架的路徑解析都是按照Ant的風(fēng)格來(lái)的;
(2)AntPathMatcher不僅可以匹配Spring的@RequestMapping路徑,也可以用來(lái)匹配各種字符串,包括文件路徑等。
基本規(guī)則
(1)? 匹配一個(gè)字符(除過(guò)操作系統(tǒng)默認(rèn)的文件分隔符)
(2)* 匹配0個(gè)或多個(gè)字符
(3)**匹配0個(gè)或多個(gè)目錄
(4){spring:[a-z]+} 將正則表達(dá)式[a-z]+匹配到的值,賦值給名為 spring 的路徑變量.
(PS:必須是完全匹配才行,在SpringMVC中只有完全匹配才會(huì)進(jìn)入controller層的方法)
注意事項(xiàng)
(1)匹配文件路徑,需要匹配某目錄下及其各級(jí)子目錄下所有的文件,使用/**/*而非*.*,因?yàn)橛械奈募灰欢ê形募缶Y;
(2)匹配文件路徑,使用AntPathMatcher創(chuàng)建一個(gè)對(duì)象時(shí),需要注意AntPathMatcher也有有參構(gòu)造,傳遞路徑分隔符參數(shù)pathSeparator,對(duì)于文件路徑的匹配來(lái)說(shuō),則需要根據(jù)不同的操作系統(tǒng)來(lái)傳遞各自的文件分隔符,以此防止匹配文件路徑錯(cuò)誤。源碼截圖如下:
可以看到,AntPathMatcher默認(rèn)路徑分隔符為“/”,而在匹配文件路徑時(shí),需要注意Windows下路徑分隔符為“\”,Linux下為“/”,寫法即為:
AntPathMatcher matcher = new AntPathMatcher(File.separator); AntPathMatcher matcher = new AntPathMatcher(System.getProperty("file.separator"));
(3)最長(zhǎng)匹配規(guī)則(has more characters),即越精確的模式越會(huì)被優(yōu)先匹配到。例如,URL請(qǐng)求/app/dir/file.jsp,現(xiàn)在存在兩個(gè)路徑匹配模式/**/*.jsp和/app/dir/*.jsp,那么會(huì)根據(jù)模式/app/dir/*.jsp來(lái)匹配。
測(cè)試用例
// 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進(jìn)行授權(quán)認(rèn)證時(shí),有些請(qǐng)求url需要過(guò)濾掉,針對(duì)帶/service/{id}/user-info這種帶操作符的請(qǐng)求,需要特殊處理----AntPathMatcher就上場(chǎng)啦
具體使用場(chǎng)景
1.登錄授權(quán)驗(yàn)證:過(guò)濾掉登錄請(qǐng)求,一些資源獲取請(qǐng)求
2.請(qǐng)求接口日志打?。哼^(guò)濾掉文件上傳和下載的一些請(qǐng)求,requestBody里的文件流會(huì)被異常修改
具體代碼:
請(qǐng)求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())); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springmvc json類型轉(zhuǎn)換錯(cuò)誤解決方案
這篇文章主要介紹了springmvc json類型轉(zhuǎn)換錯(cuò)誤解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例
本篇文章介紹了java仿windows 字體設(shè)置選項(xiàng)卡,可實(shí)現(xiàn)類似windows字體設(shè)置效果,需要的朋友可以參考下。2016-10-10Java transient關(guān)鍵字與序列化操作實(shí)例詳解
這篇文章主要介紹了Java transient關(guān)鍵字與序列化操作,結(jié)合實(shí)例形式詳細(xì)分析了java序列化操作相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2019-09-09idea指定maven的settings文件不生效的問(wèn)題解決
本文主要介紹了idea指定maven的settings文件不生效的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java虛擬機(jī)JVM類加載機(jī)制(從類文件到虛擬機(jī))
所謂的類加載機(jī)制就是虛擬機(jī)將class文件加載到內(nèi)存,并對(duì)數(shù)據(jù)進(jìn)行驗(yàn)證,轉(zhuǎn)換解析和初始化,形成虛擬機(jī)可以直接使用的java類型,本文給大家介紹類加載機(jī)制過(guò)程從類文件到虛擬機(jī)的詳細(xì)說(shuō)明,感興趣的朋友跟隨小編一起看看吧2021-06-06idea 解決用骨架創(chuàng)建項(xiàng)目過(guò)慢的操作方式
這篇文章主要介紹了idea 解決用骨架創(chuàng)建項(xiàng)目過(guò)慢的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java Comparable和Comparator對(duì)比詳解
這篇文章主要介紹了Java Comparable和Comparator對(duì)比詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java基于HttpClient實(shí)現(xiàn)RPC的示例
HttpClient可以實(shí)現(xiàn)使用Java代碼完成標(biāo)準(zhǔn)HTTP請(qǐng)求及響應(yīng)。本文主要介紹了Java基于HttpClient實(shí)現(xiàn)RPC,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟
這篇文章主要介紹了JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟,通過(guò)將數(shù)據(jù)庫(kù)的連接放在一個(gè)工具類里面,達(dá)到重用的效果,需要的朋友可以參考下2015-07-07