🍃【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} &mdash; matches {@code com/test.jsp} but also
 * {@code com/tast.jsp} or {@code com/txst.jsp}</li>
 * <li>{@code com/*.jsp} &mdash; matches all {@code .jsp} files in the
 * {@code com} directory</li>
 * <li><code>com/&#42;&#42;/test.jsp</code> &mdash; matches all {@code test.jsp}
 * files underneath the {@code com} path</li>
 * <li><code>org/springframework/&#42;&#42;/*.jsp</code> &mdash; matches all
 * {@code .jsp} files underneath the {@code org/springframework} path</li>
 * <li><code>org/&#42;&#42;/servlet/bla.jsp</code> &mdash; 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章