使用Spring自身提供的地址匹配工具匹配URL操作
使用Spring自身提供的地址匹配工具匹配URL
public class PathMatcherUtil { /** * 實(shí)際驗(yàn)證路徑匹配權(quán)限 * * @param matchPath 權(quán)限url * @param path 訪問路徑 * @return 是否擁有權(quán)限 */ public static boolean match(String matchPath, String path) { SpringAntMatcher springAntMatcher = new SpringAntMatcher(matchPath, true); return springAntMatcher.matches(path); } /** * 實(shí)際驗(yàn)證路徑匹配權(quán)限 * * @param list 權(quán)限url * @param path 訪問路徑 * @return 是否擁有權(quán)限 */ public static boolean matches(Collection<String> list, String path) { for (String s : list) { SpringAntMatcher springAntMatcher = new SpringAntMatcher(s, true); if (springAntMatcher.matches(path)) { return true; } } return false; } /** * 地址表達(dá)式匹配工具 */ private static class SpringAntMatcher implements Matcher { private final AntPathMatcher antMatcher; private final String pattern; private SpringAntMatcher(String pattern, boolean caseSensitive) { this.pattern = pattern; this.antMatcher = createMatcher(caseSensitive); } @Override public boolean matches(String path) { return this.antMatcher.match(this.pattern, path); } @Override public Map<String, String> extractUriTemplateVariables(String path) { return this.antMatcher.extractUriTemplateVariables(this.pattern, path); } private static AntPathMatcher createMatcher(boolean caseSensitive) { AntPathMatcher matcher = new AntPathMatcher(); matcher.setTrimTokens(false); matcher.setCaseSensitive(caseSensitive); return matcher; } } private interface Matcher { boolean matches(String var1); Map<String, String> extractUriTemplateVariables(String var1); } }
spring url路徑匹配用法經(jīng)驗(yàn)介紹
在web應(yīng)用中,需要對請求url路徑進(jìn)行一些判斷匹配,完成一定的功能,如進(jìn)行訪問權(quán)限的判斷,acegi就采用了路徑匹配來判斷請求url路徑是否為合法,但是沒有將api抽取出來,用起來還是依賴性太強(qiáng),不好做輕量級的擴(kuò)展。
spring提供了工具類AntPathMatcher實(shí)現(xiàn)了判斷路徑匹配,非常簡單好用,屬輕量級的組件,下面具體談一下。
先貼一段代碼來快速了解一下它的用法
(可以看一下代碼注釋,比較詳細(xì)),如下:
package test.web; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; import junit.framework.TestCase; /** * 路徑匹配 * @author yandk * @date Feb 15, 2012 */ public class TestAntPathMatcher extends TestCase{ public void testMatch(){ PathMatcher matcher = new AntPathMatcher(); // 完全路徑url方式路徑匹配 // String requestPath="請求路徑 // String patternPath="**/login.jsp";//路徑匹配模式 // 不完整路徑uri方式路徑匹配 // String requestPath="/app/pub/login.do";//請求路徑 // String patternPath="/**/login.do";//路徑匹配模式 // 模糊路徑方式匹配 // String requestPath="/app/pub/login.do";//請求路徑 // String patternPath="/**/*.do";//路徑匹配模式 // 包含模糊單字符路徑匹配 String requestPath="/app/pub/login.do";//請求路徑 String patternPath="/**/lo?in.do";//路徑匹配模式 boolean result =matcher.match(patternPath, requestPath); assertTrue(result); }
注:以上代碼取消注釋的片段,都能通過測試,使用時可根據(jù)具體情況調(diào)整即可。
總結(jié)如下
ANT方式的通配符有三種
- ?(匹配任何單字符)
- *(匹配0或者任意數(shù)量的字符)
- **(匹配0或者更多的目錄)
url路徑匹配規(guī)則
URL路徑 | 說明 |
/app/*.x | 匹配(Matches)所有在app路徑下的.x文件 |
/app/p?ttern | 匹配(Matches) /app/pattern 和 /app/pXttern,但是不包括/app/pttern |
/**/example | 匹配(Matches) /app/example, /app/foo/example, 和 /example |
/app/**/dir/file. | 匹配(Matches) /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java |
/**/*.jsp | 匹配(Matches)任何的.jsp 文件 |
最長匹配原則(has more characters)
說明,URL請求/app/dir/file.jsp,現(xiàn)在存在兩個路徑匹配模式/**/*.jsp和/app/dir/*.jsp,那么會根據(jù)模式/app/dir/*.jsp來匹配
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring簡單MVC實(shí)現(xiàn)方法(URL映射及其參數(shù)使用、查詢(id、其他參數(shù))、增加)
這篇文章主要介紹了spring簡單MVC實(shí)現(xiàn)方法(URL映射及其參數(shù)使用、查詢(id、其他參數(shù))、增加),方法參數(shù)使用包括在無注解下獲取參數(shù),使用@RequestParam 獲取參數(shù)的方法,每種方法講解的非常詳細(xì),需要的朋友可以參考下2024-01-01Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋
這篇文章主要介紹了Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06Java?深入學(xué)習(xí)static關(guān)鍵字和靜態(tài)屬性及方法
這篇文章主要介紹了Java?深入學(xué)習(xí)static關(guān)鍵字和靜態(tài)屬性及方法,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09