🍃【Spring專題】「技術原理」為大家介紹一下Spring中的Ant路徑匹配工具元件AntPathMatcher
Spring中的絕大多數的路徑匹配規則是依照Ant的標準來的
實際上不只是SpringMVC,整個Spring框架的路徑解析都是按照Ant的風格來的,在Spring中的具體實現,詳情參見 org.springframework.util.AntPathMatcher,具體規則如下
/** * {@link PathMatcher} implementation for Ant-style path patterns. * * <p>Part of this mapping code has been kindly borrowed from <a href="http://ant.apache.org">Apache Ant</a>. * * <p>The mapping matches URLs using the following rules:<br> * <ul> * <li>{@code ?} matches one character</li> * <li>{@code *} matches zero or more characters</li> * <li>{@code **} matches zero or more <em>directories</em> in a path</li> * <li>{@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"</li> * </ul> * * <h3>Examples</h3> * <ul> * <li>{@code com/t?st.jsp} — matches {@code com/test.jsp} but also * {@code com/tast.jsp} or {@code com/txst.jsp}</li> * <li>{@code com/*.jsp} — matches all {@code .jsp} files in the * {@code com} directory</li> * <li><code>com/**/test.jsp</code> — matches all {@code test.jsp} * files underneath the {@code com} path</li> * <li><code>org/springframework/**/*.jsp</code> — matches all * {@code .jsp} files underneath the {@code org/springframework} path</li> * <li><code>org/**/servlet/bla.jsp</code> — matches * {@code org/springframework/servlet/bla.jsp} but also * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}</li> * <li>{@code com/{filename:\\w+}.jsp} will match {@code com/test.jsp} and assign the value {@code test} * to the {@code filename} variable</li> * </ul> * * <p><strong>Note:</strong> a pattern and a path must both be absolute or must * both be relative in order for the two to match. Therefore it is recommended * that users of this implementation to sanitize patterns in order to prefix * them with "/" as it makes sense in the context in which they're used. * /
符號的規則定義標準
- ? 匹配1個字元
-
- 匹配0個或多個字元
- ** 匹配路徑中的0個或多個目錄
- {spring:[a-z]+} 將正則表示式[a-z]+匹配到的值,賦值給名為spring的路徑變數。
必須是完全匹配才行,在SpringMVC中只有完全匹配才會進入controller層的方法
符號 ?
和其它幾個不一樣的是? 要求必須為一個字元,並且不能是代表路徑分隔符的/。
@RequestMapping("/index?")
@ResponseBody
public String index(){
return "index.html";
}
結果
index false 404錯誤(必須要有一個字元) index/ false 404錯誤(不能為"/") indexab false 404錯誤(不能是多個字元) indexa true 輸出頁面index.html
符號 *
- ,雖然可以匹配多個任意的字元,但是,無法用 * 可以替代 ** ,因為 * 代表的多個任意字元組成的字串不能是個目錄或者說路徑.也就是說,* 並不能拿來替代 **.
示例程式碼:
@RequestMapping("/index*")
@ResponseBody
public String index(){
return "index.html";
}
結果:
index true 輸出index.html(可以為0字元) index/ true 輸出 index.html(可以為"/") indexa true 輸出 index.html(可以為1個字元) indexabc true 輸出 index.html(可以為多個字元) index/a false 404錯誤("/a"是一個路徑)
符號 **
0個或多個目錄.** 代表的字串本身不一定要包含 /
@RequestMapping("/index/**/a")
@ResponseBody
public String index(){
return "index.html";
}
結果:
index/a true 輸出 index.html(可以為0個目錄)
index/x/a true 輸出 index.html(可以為一個目錄)
index/x/z/c/a true 輸出 index.html(可以為多個目錄)
符號 {spring:[a-z]+}
其它的關於 AntPathMatcher 的文章裡,對 {spring:[a-z]+} 的匹配大多是隻字未提.這裡補充下.
示例程式碼:
@RequestMapping("/index/{username:[a-b]+}")
@ResponseBody
public String index(@PathVariable("username") String username){
System.out.println(username);
return username;
}
結果:
index/ab true 輸出 ab
index/abbaaa true 輸出 abbaaa
index/a false 404錯誤
index/ac false 404錯誤
需求:我在做rbac許可權校驗的時候,設定管理員的訪問路徑為/admin/**,希望所有的開頭為/admin/的uri操作地址都能進行匹配判斷。
手動使用方式
- AntPathMatcher不僅可以匹配Spring的@RequestMapping路徑,也可以用來匹配各種字串,包括檔案路徑等。
- AntPathMatcher預設路徑分隔符為“/”,而在匹配檔案路徑時,需要注意Windows下路徑分隔符為“\”,Linux下為“/”,寫法即為:
初始化建立操作
匹配檔案路徑,使用AntPathMatcher建立一個物件時,需要注意AntPathMatcher也有有參構造,傳遞路徑分隔符引數pathSeparator,對於檔案路徑的匹配來說,則需要根據不同的作業系統來傳遞各自的檔案分隔符,以此防止匹配檔案路徑錯誤。
AntPathMatcher matcher = new AntPathMatcher(File.separator);
AntPathMatcher matcher = new AntPathMatcher(System.getProperty("file.separator"));
執行匹配操作
import org.springframework.util.AntPathMatcher;
String content = "/admin/acuff";
String pattern = "/admin/**";
System.out.println(antPathMatcher.match(pattern, content));
最長匹配原則(has more characters)
最長匹配規則(has more characters),即越精確的模式越會被優先匹配到。例如,URL請求/app/dir/file.jsp,現在存在兩個路徑匹配模式/**/.jsp和/app/dir/.jsp,那麼會根據模式/app/dir/*.jsp來匹配。
當然如果覺得這個工具還不夠強大,還可以使用RegexRequestMatcher ,它支援使用正則表示式對URL地址進行匹配。如果你覺得這些都不夠強大可以自己重寫 RequestMatcher介面來進行定製的路由匹配規則
摘取網上的案例參考Sample
// 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 mvc url地址匹配工具類
AntPathRequestMatcher
在spring mvc 中我們會經常使用//.jsp、/app//dir/file.、/**/example 、/app/.x 類似於這樣語法而負責真正判斷是否匹配的工具類就是AntPathRequestMatcher
極限就是為了超越而存在的