SpringSecurity-AntPathMatcher-Rest-Match

简介

使用Spring的AntPathMatch对RESTful的URL进行匹配.

匹配规则

ANT方式的通配符有三种:

  • ? 匹配任何单字符
  • * 匹配0或者任意数量的字符
  • ** 匹配0或者更多的目录

注: 所有的通配符均不包含路径分隔符/.

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;

/**
* AntPathMatchTest :
*
* @author http://arccode.net
* @since 2015-04-14 23:12
*/

public class AntPathMatchTest {

private final Logger logger = LoggerFactory.getLogger(AntPathMatchTest.class);

@Test
public void test() {

AntPathMatcher matcher = new AntPathMatcher();
String[] currUrls = {"/bop/wallpapers", "/bop/wallpapers1", "/bop/wallpapers11", "/bop/wallpapers/1", "/bop/wallpapers/1/labels/2"};
String[] userAuths = {"/bop/wallpapers", "/bop/wallpapers?","/bop/wallpapers/*", "/bop/wallpapers/**"};

for (String currUrl: currUrls) {
for (String userAuth: userAuths) {
logger.info(currUrl + "-----" + userAuth + "-----" + matcher.match(userAuth, currUrl) + "");
}
}
}
}

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.