广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用Spring自身提供的地址匹配工具匹配URL操作
  • 922
分享到

使用Spring自身提供的地址匹配工具匹配URL操作

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

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

摘要

目录使用spring自身提供的地址匹配工具匹配URLspring url路径匹配用法经验介绍先贴一段代码来快速了解一下它的用法总结如下ANT方式的通配符有三种url路径匹配规则使用S

使用Spring自身提供的地址匹配工具匹配URL


public class PathMatcherUtil {
    
    public static boolean match(String matchPath, String path) {
        SpringAntMatcher springAntMatcher = new SpringAntMatcher(matchPath, true);
        return springAntMatcher.matches(path);
    }
    
    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;
    }
    
    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);
    }
}

spring url路径匹配用法经验介绍

WEB应用中,需要对请求url路径进行一些判断匹配,完成一定的功能,如进行访问权限的判断,acegi就采用了路径匹配来判断请求url路径是否为合法,但是没有将api抽取出来,用起来还是依赖性太强,不好做轻量级的扩展。

spring提供了工具类AntPathMatcher实现了判断路径匹配,非常简单好用,属轻量级的组件,下面具体谈一下。

先贴一段代码来快速了解一下它的用法

(可以看一下代码注释,比较详细),如下:


package test.web;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import junit.framework.TestCase;                                                                                          

public class TestAntPathMatcher extends TestCase{
 public void testMatch(){
  PathMatcher  matcher = new AntPathMatcher();
//  完全路径url方式路径匹配                                                                                            
//  String requestPath="请求路径                                                                                                                                                                                            
//  String patternPath="**/login.jsp";//路径匹配模式
  
//  不完整路径uri方式路径匹配
//  String requestPath="/app/pub/login.do";//请求路径
//  String patternPath="login.do";//路径匹配模式
//  模糊路径方式匹配
//  String requestPath="/app/pub/login.do";//请求路径
//  String patternPath="*.do";//路径匹配模式
//  包含模糊单字符路径匹配
  String requestPath="/app/pub/login.do";//请求路径
  String patternPath="lo?in.do";//路径匹配模式
  boolean result =matcher.match(patternPath, requestPath);
  assertTrue(result);
 }

注:以上代码取消注释的片段,都能通过测试,使用时可根据具体情况调整即可。

总结如下

ANT方式的通配符有三种

  • ?(匹配任何单字符)
  • *(匹配0或者任意数量的字符)
  • **(匹配0或者更多的目录)

url路径匹配规则

URL路径 说明
/appexample 匹配(Matches) /app/example, /app/foo/example, 和 /example
/appdir/file. 匹配(Matches) /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java
*.jsp 匹配(Matches)任何的.jsp 文件

最长匹配原则(has more characters)

说明,URL请求/app/dir/file.jsp,现在存在两个路径匹配模式*.jsp和/app/dir/*.jsp,那么会根据模式/app/dir/*.jsp来匹配

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

--结束END--

本文标题: 使用Spring自身提供的地址匹配工具匹配URL操作

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

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

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

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

下载Word文档
猜你喜欢
  • 使用Spring自身提供的地址匹配工具匹配URL操作
    目录使用Spring自身提供的地址匹配工具匹配URLspring url路径匹配用法经验介绍先贴一段代码来快速了解一下它的用法总结如下ANT方式的通配符有三种url路径匹配规则使用S...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作