This is the eleventh day of my participation in the August More Text Challenge. For details, see August more Text Challenge

Path matching problem

Problem description

In the development process, we usually solve some common problems in interceptors, such as url based permission verification, the common way is to determine whether the current is equal to the specified,

With the increase of business, there will be more and more interception, so how to gracefully intercept?

The solution

Solution 1: Use regular expressions

The advantage of using regular expressions for fuzzy matching is that it is very flexible, and you can play it however you want. The downside is that it is too flexible to exhaustive, and you need to encapsulate the rules yourself.

Solution 2: Use the tool classes in the Spring Framework project

AntPathMatcher is a tool helper class, to help us match the path is to meet, refer to the Apache Ant project path matching rules, so you must first understand the Apache ant matching rules are as follows:

Online is not authoritative enough, most of them are the same, here the author specially went to the official website to check, referring to the ant-style path matching pattern syntax

Example Explanation
**/CVS/* Matches all files in directories that can be located anywhere in the directory tree. Matches: But not: ( part does not match) CVS CVS/Repository; org/apache/CVS/Entries; org/apache/jakarta/tools/ant/CVS/Entries; org/apache/CVS/foo/bar/Entries foo/bar/.
org/apache/jakarta/** Matches all files in the directory tree.Matches: But not: ( part is missing).org/apache/jakarta; org/apache/jakarta/tools/ant/docs/index.html; org/apache/jakarta/test.xml; org/apache/xyz.java; jakarta/
org/apache/**/CVS/* Matches all files in directories that are located anywhere in the directory tree under .Matches: But not: ( part does not match)CVSorg/apache; org/apache/CVS/Entries; org/apache/jakarta/tools/ant/CVS/Entries; org/apache/CVS/foo/bar/Entries; foo/bar/.
**/test/** Matches all files that have a element in their path, including as a filename.testtest

However, Spring’s AntPathMatcher is much more powerful than that

Here I do not post AntPathMatcherTests source code, as well as a few tables.

Match, matchStart methods

It is used to check whether the match is successful

Test exact match
expression Matched path Execution result (Boolean)
test test true
/test test false
test /test false

Test wildcards?(Can only match one character)
expression Matched path Execution result (Boolean)
t? st test true
t??? st tedfst true
tes? test true
tes? testt false
? es? test true

Test wildcards*(Matches n characters)
expression Matched path Execution result (Boolean)
* test true
test* test true
test* testTest true
test* test/ false
test/* test/Test true
test/* test/ true
test/* test false
*test* AnothertestTabc true
*test Anothertest true
*. * test. true
*. * test.test true
test*aaa testblaaaa true

Test wildcards支那(Matches n characters)
expression Matched path Execution result (Boolean)
/ * * /testing/testing true
/ * / * * /testing/testing true
/ / * * * /testing/testing true
/bla/**/bla /bla/testing/testing/bla/bla true
/bla*bla/test /blaXXXbla/test true
/*bla*/**/bla/** /XXXblaXXXX/testing/testing/bla/testing/testing.jpg true

ExtractPathWithinPattern method

Returns the part of the blur match

expression Matched path Execution result (String)
/docs/* /docs/cvs/commit cvs/commit
/d? cs/**/*.html /docs/cvs/commit.html docs/cvs/commit.html

ExtractUriTemplateVariables method

It is mainly used to get the value in the path {var}

expression Matched path Execution result (Map)
/hotels/{hotel} /hotels/1 {“hotel”:”1″}
/{name}.{extension} /test.html {“name”:”test”,”extension”:”html”}
/A-{B}-C /A-b-C {“B”:”b”}