基于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
0
投稿
猜你喜欢
- 前言在实际开发中我们经常会与时间打交道,那这就会涉及到一个时间格式转换的问题。接下来会介绍几种在SpirngBoot中如何对时间格式进行转换
- 实例如下:public String stripHtml(String content) { // <p>段落替换为换行 con
- .net core提供了Json处理模块,在命名空间System.Text.Json中,下面通过顶级语句,对C#的Json功能进行讲解。序列
- 绘制圆形锯齿问题普通绘制圆形形状时可以看到图形边缘会有明显锯齿现象并不像真实圆形形状一样圆润边缘平滑。在glsl中这种情况是常见情况,这里是
- 本文实例讲述了Spring使用@Resource配置依赖操作。分享给大家供大家参考,具体如下:一 配置<?xml version=&q
- 线程的两种创建方式及优劣比较1、通过实现Runnable接口线程创建(1).定义一个类实现Runnable接口,重写接口中的run()方法。
- 集成配置步骤步骤1:加入 Maven 相关依赖<!-- 指定 Springboot 版本 --><parent> &
- 目录前言1. 如何让两个线程依次执行?2. 如何让两个线程按照指定的方式有序相交?3. 线程 D 在A、B、C都同步执行完毕后执行4. 三个
- mybatis if test判断BigDecimal遇到的坑<update id="test" paramete
- 一、下载rocketmq对应版本源码修改消息存储路径需要修改rocketmq源码,因为rocketmq取的默认路径是user.home路径,
- 网络应用分为客户端和服务端两部分,而Socket类是负责处理客户端通信的Java类。通过这个类可以连接到指定IP或域名的服务器上,并且可以和
- IntInt是Java八种基本数据类型之一,一般大小为4字节32位,取值范围为2-31—231。两个Int类型变量用“==”比较的是值的大小
- java实现接口签名为了保证数据传输的安全性,跟其他系统进行数据交互时,双方应该约定好密钥,把数据进行加密,接口签名,这样双方调用接口时,验
- 归纳一些网上取JAVA路径的方法: 注明:如果从ANT启动程序,this.getClass().getResource("&quo
- 前提:集成开发环境(IDE):eclipsejdk版本:8.0File类的几个方法:1)isFile()测试此抽象路径名表示的文件是否为普通
- Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, doub
- 用C#想写一个直接将数据库查询得到的datatable,直接导出为csv格式的文件,拷贝到导出的操作类后,一直catch到的错误提示是对路径
- TreeMap 的实现就是红黑树数据结构,也就说是一棵自平衡的排序二叉树,这样就可以保证当需要快速检索指定节点。TreeSet 和 Tree
- 简介Trie树,又称为前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由
- 本文实例讲述了Java Socket实现单线程通信的方法。分享给大家供大家参考,具体如下:现在做Java直接使用Socket的情况是越来越少