Shiro系列教程URL匹配規則(AntPathMatcher)

官方示例

...
[urls]

/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]


匹配規則

?  匹配一個字符
*  匹配一個或多個字符
** 匹配一個或多個目錄

com/t?st.jsp 匹配 com/test.jsp but also com/tast.jsp or com/txst.jsp 
PatternMatcher matcher = new AntPathMatcher();
System.out.println(matcher.matches("/com/t?st.jsp", "/com/txst.jsp"));
System.out.println(matcher.matches("/com/t?st.jsp", "/com/tast.jsp"));
System.out.println(matcher.matches("/com/t?st.jsp", "/com/txst.jsp"));

com/*.jsp 匹配com目錄下任何.jsp文件
PatternMatcher matcher = new AntPathMatcher();
System.out.println(matcher.matches("com/*.jsp", "com/txst.jsp"));
System.out.println(matcher.matches("com/*.jsp", "com/tast.jsp"));
System.out.println(matcher.matches("com/*.jsp", "com/txst.jsp"));

com/**/test.jsp 匹配 com目錄下任何text.jsp文件
PatternMatcher matcher = new AntPathMatcher();
System.out.println(matcher.matches("com/**/test.jsp", "com/a/test.jsp"));
System.out.println(matcher.matches("com/**/test.jsp", "com/b/test.jsp"));
System.out.println(matcher.matches("com/**/test.jsp", "com/c/d/test.jsp"));

org/apache/shiro/**/*.jsp  匹配org/apache/shiro 目錄下任何jsp文件
PatternMatcher matcher = new AntPathMatcher();
System.out.println(matcher.matches("org/apache/shiro/**/*.jsp ", "org/apache/shiro/test.jsp"));
System.out.println(matcher.matches("org/apache/shiro/**/*.jsp ", "org/apache/shiro/sub1/sub1.jsp"));
System.out.println(matcher.matches("org/apache/shiro/**/*.jsp ", "org/apache/shiro/sub2/sub2.jsp"));

org/**/servlet/bla.jsp   匹配 org/apache/shiro/servlet/bla.jsp  ||  org/apache/shiro/testing/servlet/bla.jsp || org/servlet/bla.jsp 
PatternMatcher matcher = new AntPathMatcher();
System.out.println(matcher.matches("org/**/servlet/bla.jsp", "org/apache/shiro/servlet/bla.jsp"));
System.out.println(matcher.matches("org/**/servlet/bla.jsp", "org/apache/shiro/testing/servlet/bla.jsp"));
System.out.println(matcher.matches("org/**/servlet/bla.jsp", "org/servlet/bla.jsp"));


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