iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >基于Spring概念模型:PathMatcher 路径匹配器
  • 276
分享到

基于Spring概念模型:PathMatcher 路径匹配器

2024-04-02 19:04:59 276人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录概述PathMatcher接口源代码AntPathMatcher使用例子spring的路径匹配工具 AntPathMatcher以下代码为本人使用过的路径匹配工具代码核心代码是这

源代码版本 : 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 {
	
	boolean isPattern(String path);
	
	boolean match(String pattern, String path);
	
	boolean matchStart(String pattern, String path);
	
	String extractPathWithinPattern(String pattern, String path);
	
	Map<String, String> extractUriTemplateVariables(String pattern, String path);
	
	Comparator<String> getPatternComparator(String path);
	
	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 方法去匹配路径,不需要做鉴权的接口就会被匹配到,然后继续执行非鉴权的业务流程。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 基于Spring概念模型:PathMatcher 路径匹配器

本文链接: https://www.lsjlt.com/news/135016.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作