Match URL operations using the address matching tool provided by Spring itself

  • 2021-11-10 09:32:51
  • OfStack

Directory uses the address matching tool provided by Spring itself to match URLspring url path matching usage experience introduction. First post a code to quickly understand its usage under 1. Summarize as follows. Wildcard characters in ANT mode have three url path matching rules

Match URL using the address matching tool provided by Spring itself


public class PathMatcherUtil {
    /**
     *  Actual verification path matching permission 
     *
     * @param matchPath  Authority url
     * @param path       Access path 
     * @return  Do you have permissions 
     */
    public static boolean match(String matchPath, String path) {
        SpringAntMatcher springAntMatcher = new SpringAntMatcher(matchPath, true);
        return springAntMatcher.matches(path);
    }
    /**
     *  Actual verification path matching permission 
     *
     * @param list  Authority url
     * @param path  Access path 
     * @return  Do you have permissions 
     */
    public static boolean matches(Collection<String> list, String path) {
        for (String s : list) {
            SpringAntMatcher springAntMatcher = new SpringAntMatcher(s, true);
            if (springAntMatcher.matches(path)) {
                return true;
            }
        }
        return false;
    }
    /**
     *  Address expression matching tool 
     */
    private static class SpringAntMatcher implements Matcher {
        private final AntPathMatcher antMatcher;
        private final String pattern;
        private SpringAntMatcher(String pattern, boolean caseSensitive) {
            this.pattern = pattern;
            this.antMatcher = createMatcher(caseSensitive);
        }
        @Override
        public boolean matches(String path) {
            return this.antMatcher.match(this.pattern, path);
        }
        @Override
        public Map<String, String> extractUriTemplateVariables(String path) {
            return this.antMatcher.extractUriTemplateVariables(this.pattern, path);
        }
        private static AntPathMatcher createMatcher(boolean caseSensitive) {
            AntPathMatcher matcher = new AntPathMatcher();
            matcher.setTrimTokens(false);
            matcher.setCaseSensitive(caseSensitive);
            return matcher;
        }
    }
    private interface Matcher {
        boolean matches(String var1);
        Map<String, String> extractUriTemplateVariables(String var1);
    }
}

Introduction of spring url path matching usage experience

In the application of web, it is necessary to judge and match the path of requesting url, and complete the function of one decision. For example, judging the access authority, acegi uses path matching to judge whether the path of requesting url is legal, but api is not extracted, which is still too dependent to be extended lightweight.

spring provides a tool class AntPathMatcher to achieve the judgment of path matching, very simple and easy to use, is a lightweight component, the following specific talk about 1.

First post a piece of code to quickly understand its usage

(You can see the code comments under 1, which are more detailed), as follows:


package test.web;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import junit.framework.TestCase;                                                                                          
/**
 *  Path matching 
 * @author yandk
 * @date Feb 15, 2012
 */
public class TestAntPathMatcher extends TestCase{
 public void testMatch(){
  PathMatcher  matcher = new AntPathMatcher();
//   Complete path url Mode path matching                                                                                             
//  String requestPath=" Request path                                                                                                                                                                                             
//  String patternPath="**/login.jsp";// Path matching pattern 
  
//   Incomplete path uri Mode path matching 
//  String requestPath="/app/pub/login.do";// Request path 
//  String patternPath="/**/login.do";// Path matching pattern 
//   Fuzzy path mode matching 
//  String requestPath="/app/pub/login.do";// Request path 
//  String patternPath="/**/*.do";// Path matching pattern 
//   Include fuzzy single character path matching 
  String requestPath="/app/pub/login.do";// Request path 
  String patternPath="/**/lo?in.do";// Path matching pattern 
  boolean result =matcher.match(patternPath, requestPath);
  assertTrue(result);
 }

Note: The above code unannotated fragments can pass the test, and can be adjusted according to the specific situation when using.

Summarized as follows

There are three wildcard characters in ANT mode ? (Matches any single character) * (Match 0 or any number of characters) ** (Match 0 or more directories)

url Path Matching Rules
URL路径 说明
/app/*.x 匹配(Matches)所有在app路径下的.x文件
/app/p?ttern 匹配(Matches) /app/pattern 和 /app/pXttern,但是不包括/app/pttern
/**/example 匹配(Matches) /app/example, /app/foo/example, 和 /example
/app/**/dir/file. 匹配(Matches) /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java
/**/*.jsp 匹配(Matches)任何的.jsp 文件

Maximum Matching Principle (has more characters)

Explanation: URL requests /app/dir/file.jsp, and now there are two path matching patterns /**/*. jsp and/app/dir/*. jsp, then it will be matched according to schema/app/dir/*. jsp


Related articles: