最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年09月08日 10:37:08   作者:安迪源文  
這篇文章主要介紹了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框架自身對概念模型接口也提供了一個缺省的實現(xiàn)AntPathMatcher,用于匹配Ant風(fēng)格的路徑。

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 不是一個模式,而是一個靜態(tài)路徑(真正的路徑字符串),
	 * 那么就不用調(diào)用方法 #match 了,因為對于靜態(tài)路徑的匹配,直接使用字符串等號比較就足夠了。
	 * @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.
	 * 根據(jù)當(dāng)前 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.
	 * 根據(jù)當(dāng)前 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.
	 * 該方法用于發(fā)現(xiàn)路徑中的哪一部分是和模式能動態(tài)匹配上的部分。它會去除路徑中開頭靜態(tài)部分,
	 * 僅僅返回那部分真正和模式匹配的上的部分。
	 * 例子 : "myroot/*.html" 為 pattern , "myroot/myfile.html" 為路徑,
	 *  則該方法返回 "myfile.html".  
	 * 具體的檢測規(guī)則根據(jù)當(dāng)前 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 ,
     * 內(nèi)容為 : "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.
	 * 合并兩個模式。具體合并的算法由實現(xiàn)類決定。
	 * @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);
}

從接口代碼來理解概念還是有些抽象,下面我們列舉一些基于實現(xiàn)類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();

以下代碼為本人使用過的路徑匹配工具代碼

方便以后項目中使用參考:

//不需要鑒權(quán)的接口
    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))

獲取到需要排除鑒權(quán)接口列表的接口,然后通過 AntPathMatcher 的 match 方法去匹配路徑,不需要做鑒權(quán)的接口就會被匹配到,然后繼續(xù)執(zhí)行非鑒權(quán)的業(yè)務(wù)流程。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決mybatis一對多關(guān)聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題

    解決mybatis一對多關(guān)聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題

    這篇文章主要介紹了解決mybatis一對多關(guān)聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java使用Cipher類實現(xiàn)加密的過程詳解

    Java使用Cipher類實現(xiàn)加密的過程詳解

    這篇文章主要介紹了Java使用Cipher類實現(xiàn)加密的過程詳解,Cipher類提供了加密和解密的功能,創(chuàng)建密匙主要使用SecretKeySpec、KeyGenerator和KeyPairGenerator三個類來創(chuàng)建密匙。感興趣可以了解一下
    2020-07-07
  • SpringBoot中application.properties與application.yml區(qū)別小結(jié)

    SpringBoot中application.properties與application.yml區(qū)別小結(jié)

    本文主要介紹了SpringBoot中application.properties與application.yml區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • Spring?Boot+RabbitMQ?通過fanout模式實現(xiàn)消息接收功能(支持消費者多實例部署)

    Spring?Boot+RabbitMQ?通過fanout模式實現(xiàn)消息接收功能(支持消費者多實例部署)

    這篇文章主要介紹了Spring?Boot+RabbitMQ?通過fanout模式實現(xiàn)消息接收(支持消費者多實例部署),本文通過案例場景分析給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • RocketMQ根據(jù)Tag進行消息過濾

    RocketMQ根據(jù)Tag進行消息過濾

    消費者訂閱了某個主題后,Apache RocketMQ 會將該主題中的所有消息投遞給消費者。若消費者只需要關(guān)注部分消息,可通過設(shè)置過濾條件在 Apache RocketMQ 服務(wù)端進行過濾,只獲取到需要關(guān)注的消息子集,避免接收到大量無效的消息
    2023-02-02
  • Spring bean生命周期配置過程解析

    Spring bean生命周期配置過程解析

    這篇文章主要介紹了Spring bean生命周期配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Spring實戰(zhàn)之使用XML方式管理聲明式事務(wù)操作示例

    Spring實戰(zhàn)之使用XML方式管理聲明式事務(wù)操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之使用XML方式管理聲明式事務(wù)操作,結(jié)合實例形式詳細分析了Spring XML方式管理聲明式事務(wù)具體步驟、配置、接口及使用技巧,需要的朋友可以參考下
    2020-01-01
  • 如何自定義springboot-starter日志組件供各個服務(wù)使用(系統(tǒng)日志優(yōu)化)

    如何自定義springboot-starter日志組件供各個服務(wù)使用(系統(tǒng)日志優(yōu)化)

    文章介紹了如何將各個微服務(wù)的接口調(diào)用日志邏輯優(yōu)化為一個可共享的Spring Boot Starter,通過自定義注解和自動裝配機制實現(xiàn),本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Java經(jīng)典排序算法之快速排序代碼實例

    Java經(jīng)典排序算法之快速排序代碼實例

    這篇文章主要介紹了Java經(jīng)典排序算法之快速排序代碼實例,快速排序?qū)崿F(xiàn)的思想是指通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對這兩部分?jǐn)?shù)據(jù)分別進行快速排序,需要的朋友可以參考下
    2023-10-10
  • java easyUI實現(xiàn)自定義網(wǎng)格視圖實例代碼

    java easyUI實現(xiàn)自定義網(wǎng)格視圖實例代碼

    這篇文章主要給大家介紹了關(guān)于java easyUI實現(xiàn)自定義網(wǎng)格視圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10

最新評論

白玉县| 琼海市| 永仁县| 金塔县| 上杭县| 准格尔旗| 枣庄市| 齐齐哈尔市| 革吉县| 高州市| 监利县| 中牟县| 康平县| 容城县| 南靖县| 乌兰县| 正安县| 都安| 贵南县| 巴中市| 龙井市| 广昌县| 县级市| 娄烦县| 山阴县| 红桥区| 庆阳市| 通渭县| 综艺| 磐石市| 壶关县| 怀来县| 宁强县| 定州市| 饶河县| 大兴区| 陆河县| 宣恩县| 安陆市| 新营市| 甘德县|