包路径:org.springframework.util.AntPathMatcher
工具:AntPathMatcher antPathMatcher = new AntPathMatcher();以下代码为本人使用过的路径匹配工具代码,方便以后项目中使用参考:
//不需要鉴权的接口
private Boolean excludePathFilter(String path) {
PathProperties pathProperties = (PathProperties) PathProperties.applicationContext.getBean("pathProperties");
List<String> excludePathPatterns = pathProperties.getExcludePathPatterns();
if(CollectionUtils.isEmpty(excludePathPatterns)){
return false;
}
return excludePathPatterns.stream().anyMatch(pattern -> antPathMatcher.match(pattern, path));
}
核心代码是这一行:
excludePathPatterns.stream().anyMatch(pattern -> antPathMatcher.match(pattern, path))
获取到需要排除鉴权接口列表的接口,然后通过 AntPathMatcher 的 match 方法去匹配路径,不需要做鉴权的接口就会被匹配到,然后继续执行非鉴权的业务流程。