一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 基于Spring概念模型:PathMatcher 路徑匹配器

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

2021-12-23 13:04安迪源文 Java教程

這篇文章主要介紹了Spring概念模型:PathMatcher 路徑匹配器,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

源代碼版本 : spring-webmvc-5.1.4.RELEASE

概述

PathMatcher是Spring的一個概念模型接口,該接口抽象建模了概念"路徑匹配器",一個"路徑匹配器"是一個用于路徑匹配的工具。它的使用者是 :

?
1
2
3
org.springframework.core.io.support.PathMatchingResourcePatternResolver
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
org.springframework.web.servlet.mvc.WebContentInterceptor

Spring框架自身對概念模型接口也提供了一個缺省的實現(xiàn)AntPathMatcher,用于匹配Ant風格的路徑。

PathMatcher接口源代碼

PathMatcher接口源代碼如下 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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ù)當前 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ù)當前 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ù)當前 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使用例子

?
1
2
3
4
5
6
7
8
9
10
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

包路徑:

?
1
org.springframework.util.AntPathMatcher

工具:

?
1
AntPathMatcher antPathMatcher = new AntPathMatcher();

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

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

?
1
2
3
4
5
6
7
8
9
//不需要鑒權(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));
    }

核心代碼是這一行

?
1
excludePathPatterns.stream().anyMatch(pattern -> antPathMatcher.match(pattern, path))

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

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

原文鏈接:https://andyboke.blog.csdn.net/article/details/88884286

延伸 · 閱讀

精彩推薦
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程xml與Java對象的轉(zhuǎn)換詳解

    xml與Java對象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經(jīng)有好久沒有升過級了。升級完畢重啟之后,突然發(fā)現(xiàn)好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7482021-02-04
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發(fā)項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java實現(xiàn)搶紅包功能

    Java實現(xiàn)搶紅包功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
主站蜘蛛池模板: 99这里精品| 91网红福利精品区一区二 | 喷奶水榨乳ova动漫无修 | 久久中文骚妇内射 | 国产高清自拍视频 | 福利视频导航大全 | 亚洲精品一二区 | 九九热国产视频 | 国产精品欧美日韩一区二区 | 成人高辣h视频一区二区在线观看 | 精品丰满人妻无套内射 | 欧美图片小说 | 成人私人影院在线观看网址 | 九九九九九九精品免费 | 天天天综合网 | 久久黄色免费 | 小莹的性荡生活45章 | 校园春色偷拍自拍 | 色综合网天天综合色中文男男 | 亚洲区精品| 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 | 久久久久国产一级毛片高清片 | 500第一精品 | 男女18一级大黄毛片免 | ass极品美妇pic | 免费在线电视 | 三级伦理在线播放 | 国产黑丝一区 | 欧美一级在线播放 | 韩国www | 国内自拍网红在线自拍综合 | 青青国产成人久久激情911 | 色噜噜狠狠狠综合曰曰曰88av | 精品国产乱码久久久久久人妻 | 久久99国产亚洲高清观着 | 天堂在线中文无弹窗全文阅读 | aⅴ导航站| 2020国产精品永久在线观看 | ass巨大胖女人sias | 色图18p | 51精品 |