restful鉴权白名单匹配url

6 篇文章 0 订阅
4 篇文章 0 订阅

需求

在我们开发的中会遇到这样一种情况,在鉴权的时候,需要过滤掉白名单,例如:定义有这样一个url:good/detail/{id},需要判断uri是否能通过,传统的equals方法似乎有点难(PS:如果能用equals方法解决的请在评论区告诉我)

幸好spring提供了一个很好用的类,用于匹配

AntPathMatcher antPathMatcher = new AntPathMatcher(); 
// path路径是否符合pattern的规范 
boolean match = antPathMatcher.match("/user/*", "/user/a"); 
System.out.println(match); 
match = antPathMatcher.match("/user/**", "/user/a/b"); 
System.out.println(match); 
match = antPathMatcher.match("/user/{id}", "/user/1"); System.out.println(match); 
match = antPathMatcher.match("/user/name", "/user/a"); System.out.println(match); 
boolean pattern = antPathMatcher.isPattern("user/{id}"); System.out.println(pattern); 
// 匹配是不是以path打头的地址 
boolean matchStart = antPathMatcher.matchStart("/1user/a", "/user"); System.out.println(matchStart); 
// 对路径进行合并 --> /user/a/b 
String combine = antPathMatcher.combine("/user", "a/b"); 
System.out.println(combine); 
// 找出模糊匹配中 通过*或者? 匹配上的那一段配置 
String extractPathWithinPattern = antPathMatcher.extractPathWithinPattern("/user/?", "/user/1"); 
System.out.println(extractPathWithinPattern); 
// 找出模糊匹配中 找到匹配上的项 如果匹配规则和要匹配的项规则不一致 会报错 
Map<String, String> extractUriTemplateVariables = antPathMatcher .extractUriTemplateVariables("{appName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar", "demo-sources-1.0.0.jar"); 
if (null != extractUriTemplateVariables) { 
Iterator<String> iterator = extractUriTemplateVariables.keySet().iterator(); 
while (iterator.hasNext()) { 
	String key = iterator.next(); 
	System.out.println("extractUriTemplateVariables:" + 				  extractUriTemplateVariables.get(key)); 
} 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值