using System.Text.RegularExpressions; using Microsoft.IdentityModel.Tokens; namespace Bunny.Common.Utils.Net; public static partial class NetUtil { public static List Ignores = ["/api/Login"]; /// /// 当前路径是否匹配ant path路径 /// /// ant path值 /// 当前路径 /// 是否匹配 public static bool AntPath(string pattern, string? path = "") { // 全部转成小写 path = path!.ToLower(); pattern = pattern!.ToLower(); // Ant Path匹配规则的正则表达式 var antPattern = "^" + Regex.Escape(pattern) .Replace("\\?", ".") .Replace("\\*", ".*") .Replace("\\**", ".*") + "$"; // 判断匹配规则是否成立 return path.IsNullOrEmpty() ? Regex.IsMatch("", antPattern) : Regex.IsMatch(path!, antPattern); } /// /// 是否匹配当前路径,同时判断是否在忽略列表中 /// 如果在忽略列表中,则不进行判断 /// /// /// public static bool MatchPath(string? path) { // 如果当前路径是在忽略列表中,返回未匹配 return !Ignores.Select(ignore => AntPath(ignore, path)).Any(isMatch => isMatch); } }