All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.shacocloud.mirage.web.util.PathUtil Maven / Gradle / Ivy

package cc.shacocloud.mirage.web.util;

import org.springframework.util.ObjectUtils;
import org.springframework.util.PathMatcher;

import java.util.Arrays;

public class PathUtil {

    /**
     * 根据给定路径查找是否匹配
     *
     * @param lookupPaths      查找的路径
     * @param pathMatcherToUse 用于路径模式匹配的路径匹配器
     * @param excludePatterns  排除路径模式
     * @param includePatterns  匹配路径模式
     * @return 如果拦截器应用于给定的请求路径,则为{@code true}
     */
    public static boolean matches(String[] lookupPaths,
                                  PathMatcher pathMatcherToUse,
                                  String[] excludePatterns,
                                  String[] includePatterns) {
        if (ObjectUtils.isEmpty(lookupPaths)) return false;

        if (!ObjectUtils.isEmpty(excludePatterns)) {
            for (String pattern : excludePatterns) {
                boolean anyMatch = Arrays.stream(lookupPaths).anyMatch(lookupPath -> pathMatcherToUse.match(pattern, lookupPath));
                if (anyMatch) return false;
            }
        }

        if (ObjectUtils.isEmpty(includePatterns)) return true;

        for (String pattern : includePatterns) {
            boolean anyMatch = Arrays.stream(lookupPaths).anyMatch(lookupPath -> pathMatcherToUse.match(pattern, lookupPath));
            if (anyMatch) return true;
        }
        return false;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy