spring工具類AntPathMatcher
-
package cn.bidlink.wsmp.commons.security;
-
import junit.framework.TestCase;
-
import org.springframework.util.AntPathMatcher;
-
import org.springframework.util.PathMatcher;
-
public class AntPathMatcherTest extends TestCase {
-
public void testMatch() {
-
PathMatcher matcher = new AntPathMatcher();
-
// 完全路徑url方式路徑匹配
-
String requestPath="/user/list.htm?username=aaa&departmentid=2&pageNumber=1&pageSize=20";//請求路徑
-
String patternPath="/user/list.htm**";//路徑匹配模式
-
// 不完整路徑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);
-
}
-
}
總結如下:
ANT方式的萬用字元有三種:
?(匹配任何單字元),*(匹配0或者任意數量的字元),**(匹配0或者更多的目錄)
url路徑匹配規則:
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,現在存在兩個路徑匹配模式/**/*.jsp和/app/dir/*.jsp,那麼會根據模式/app/dir/*.jsp來匹配