基于Spring概念模型:PathMatcher 路径匹配器
作者:安迪源文 发布时间:2022-08-20 12:52:38
标签:Spring,PathMatcher,路径,匹配器
源代码版本 : spring-webmvc-5.1.4.RELEASE
概述
PathMatcher是Spring的一个概念模型接口,该接口抽象建模了概念"路径匹配器",一个"路径匹配器"是一个用于路径匹配的工具。它的使用者是 :
org.springframework.core.io.support.PathMatchingResourcePatternResolver
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
org.springframework.web.servlet.mvc.WebContentInterceptor
Spring框架自身对概念模型接口也提供了一个缺省的实现AntPathMatcher,用于匹配Ant风格的路径。
PathMatcher接口源代码
PathMatcher接口源代码如下 :
package org.springframework.util;
import java.util.Comparator;
import java.util.Map;
public interface PathMatcher {
/**
* Does the given path represent a pattern that can be matched
* by an implementation of this interface?
* 判断指定的路径 path 是否是一个 pattern(模式)
* 如果返回值是 false,也就是说 path 不是一个模式,而是一个静态路径(真正的路径字符串),
* 那么就不用调用方法 #match 了,因为对于静态路径的匹配,直接使用字符串等号比较就足够了。
* @param path the path String to check
* @return true if the given path represents a pattern
*/
boolean isPattern(String path);
/**
* Match the given path against the given pattern,
* according to this PathMatcher's matching strategy.
* 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否匹配
* @param 用于检测路径字符串是否匹配于某个模式时所用的模式
* @param path 需要被检测的路径字符串
* @return true 表示匹配, false 表示不匹配
*/
boolean match(String pattern, String path);
/**
* Match the given path against the corresponding part of the given
* pattern, according to this PathMatcher's matching strategy.
* 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否之间
* 是否为前缀匹配
* @param pattern the pattern to match against
* @param path the path String to test
* @return true 表示匹配, false 表示不匹配
*/
boolean matchStart(String pattern, String path);
/**
* Given a pattern and a full path, determine the pattern-mapped part.
* 给定一个模式 pattern 和一个全路径 path,判断路径中和模式匹配的部分。
*
* This method is supposed to find out which part of the path is matched
* dynamically through an actual pattern, that is, it strips off a statically
* defined leading path from the given full path, returning only the actually
* pattern-matched part of the path.
* 该方法用于发现路径中的哪一部分是和模式能动态匹配上的部分。它会去除路径中开头静态部分,
* 仅仅返回那部分真正和模式匹配的上的部分。
* 例子 : "myroot/*.html" 为 pattern , "myroot/myfile.html" 为路径,
* 则该方法返回 "myfile.html".
* 具体的检测规则根据当前 PathMatcher 的匹配策略来顶。
* A simple implementation may return the given full path as-is in case
* of an actual pattern, and the empty String in case of the pattern not
* containing any dynamic parts (i.e. the pattern parameter being
* a static path that wouldn't qualify as an actual #isPattern pattern.
* A sophisticated implementation will differentiate between the static parts
* and the dynamic parts of the given path pattern.
* @param pattern the path pattern
* @param path the full path to introspect
* @return the pattern-mapped part of the given path
* (never null)
*/
String extractPathWithinPattern(String pattern, String path);
/**
* Given a pattern and a full path, extract the URI template variables. URI template
* variables are expressed through curly brackets ('{' and '}').
* 给定一个模式和一个路径,提取其中的 URI 模板变量信息。URI模板变量表达式格式为 "{variable}"
*
* 例子 : pattern 为 "/hotels/{hotel}" ,路径为 "/hotels/1", 则该方法会返回一个 map ,
* 内容为 : "hotel"->"1".
* @param pattern the path pattern, possibly containing URI templates
* @param path the full path to extract template variables from
* @return a map, containing variable names as keys; variables values as values
*/
Map<String, String> extractUriTemplateVariables(String pattern, String path);
/**
* Given a full path, returns a Comparator suitable for sorting patterns
* in order of explicitness for that path.
* The full algorithm used depends on the underlying implementation,
* but generally, the returned Comparator will sort a list so that more
* specific patterns come before generic patterns.
* @param path the full path to use for comparison
* @return a comparator capable of sorting patterns in order of explicitness
*/
Comparator<String> getPatternComparator(String path);
/**
* Combines two patterns into a new pattern that is returned.
* The full algorithm used for combining the two pattern depends on the underlying implementation.
* 合并两个模式。具体合并的算法由实现类决定。
* @param pattern1 the first pattern
* @param pattern2 the second pattern
* @return the combination of the two patterns
* @throws IllegalArgumentException when the two patterns cannot be combined
*/
String combine(String pattern1, String pattern2);
}
从接口代码来理解概念还是有些抽象,下面我们列举一些基于实现类AntPathMatcher的例子来增强理解 。
AntPathMatcher使用例子
AntPathMatcher antPathMatcher = new AntPathMatcher();
antPathMatcher.isPattern("/user/001");// 返回 false
antPathMatcher.isPattern("/user/*"); // 返回 true
antPathMatcher.match("/user/001","/user/001");// 返回 true
antPathMatcher.match("/user/*","/user/001");// 返回 true
antPathMatcher.matchStart("/user/*","/user/001"); // 返回 true
antPathMatcher.matchStart("/user/*","/user"); // 返回 true
antPathMatcher.matchStart("/user/*","/user001"); // 返回 false
antPathMatcher.extractPathWithinPattern("uc/profile*","uc/profile.html"); // 返回 profile.html
antPathMatcher.combine("uc/*.html","uc/profile.html"); // uc/profile.html
spring的路径匹配工具 AntPathMatcher
包路径:
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 方法去匹配路径,不需要做鉴权的接口就会被匹配到,然后继续执行非鉴权的业务流程。
来源:https://andyboke.blog.csdn.net/article/details/88884286


猜你喜欢
- 目录1 简介2 项目整合2.1 JWT工具类2.2 JWT的过滤器2.3 Security的配置2.4 获取JWT的Controller3
- 这篇文章主要介绍了SpringBoot整合Druid数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- /* * 绘制0°到360°的正弦曲线 * 分两种情形,y>0和y<=0进行绘制 * 每种情形中要
- 看门见山1.java中replace API:replace(char oldChar, char newChar):寓意为:返回一个新的字
- 本文实例讲述了WinForm实现窗体最大化并遮盖任务栏的方法。分享给大家供大家参考。具体实现方法如下:using System;using
- 本文介绍了SharedPreferences保存应用程序数据的具体步骤,供大家参考,具体内容如下1、SharedPreferences的简单
- 最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的
- 前言建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一
- 今天在做数据分页显示的时候遇到了一个问题,经过测试,证实是Tomcat 6的一个bug,我所用的版本为:apache-tomcat-6.0.
- 左右滑动是智能手机最常用的动作,在此简单的封装了一下,以后直接拿来用就可以了。简单的只需要几行就可以了,下面那个类是封装好了的。packag
- 此例子,用于说明如何在Java中对“注解 Annotation”的定义、使用和解析的操作。注解一般用于自定义开发框架中,至于为什么使用,此处
- View的平滑滚动效果什么是实现View的平滑滚动效果呢,举个简单的例子,一个View从在我们指定的时间内从一个位置滚动到另外一个位置,我们
- 一、HandlerThread的介绍及使用举例  
- 1.Feign传统方式的不足①.在微服务架构中,当我们使用Feign传统方式进行服务调用的时候,需要在每个服务消费者中添加FeignClie
- 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发概述要使用@Scheduled注解,
- 序章简介:bean的加载控制指根据特定情况对bean进行选择性加载以达到适用项目的目标。根据之前对bean加载的八种方式,其中后面四种是可以
- 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再
- 首先对于一个SpringBoot工程来说,最明显的标志的就是 @SpringBootApplication它标记了这是一个SpringBoo
- 今天在帮助客户解决一个问题时,由于他们的手机是用的5.0系统身边没有5.0系统的手机,只能用一个模拟器来安装测试应用,但是在安装过程中碰到了
- 前言:在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件