zoukankan      html  css  js  c++  java
  • JAVA判断指定url地址是否匹配指定url集合中的任意一个

    判断字符串为空和判断集合是否为空用到依赖,也可以改成自己的方式

      <!-- Spring Web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.12.RELEASE</version>
            </dependency>
    /**
         * 判断指定url地址是否匹配指定url集合中的任意一个
         *
         * @param urlPath 指定url地址
         * @param urls    需要检查的url集合
         * @return 是否匹配  匹配返回true,不匹配返回false
         */
        public static boolean matches(String urlPath, List<String> urls) {
            if (StringUtils.isEmpty(urlPath) || CollectionUtils.isEmpty(urls)) {
                return false;
            }
            for (String url : urls) {
                if (isMatch(url, urlPath)) {
                    return true;
                }
            }
            return false;
        }
    
    
        /**
         * 判断url是否与规则配置:
         * ? 表示单个字符
         * * 表示一层路径内的任意字符串,不可跨层级
         * ** 表示任意层路径
         *
         * @param url     匹配规则
         * @param urlPath 需要匹配的url
         * @return
         */
        public static boolean isMatch(String url, String urlPath) {
            AntPathMatcher matcher = new AntPathMatcher();
            return matcher.match(url, urlPath);
        }
      
    

    使用测试

        public static void main(String[] args) {
            List list = new LinkedList();
            list.add("/api/**/insert/**");
            String url = "/api/ces11/insert/11";
    
            System.out.println(matches(url, list));
        }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    牛客多校(2020第十场)E Game
    牛客多校(2020第十场)A Permutation
    牛客多校(2020第十场)A Permutation
    牛客多校(2020第九场)A Groundhog and 2-Power Representation
    牛客多校(2020第九场)A Groundhog and 2-Power Representation
    牛客多校(2020第九场)F Groundhog Looking Dowdy
    牛客多校(2020第九场)F Groundhog Looking Dowdy
    隐式转换
    正则(草稿)
    setimout
  • 原文地址:https://www.cnblogs.com/pxblog/p/15330718.html
Copyright © 2011-2022 走看看