CSharp-Single-EFCore/Bunny.Common/Utils/Net/Path.cs

44 lines
1.4 KiB
C#

using System.Text.RegularExpressions;
using Microsoft.IdentityModel.Tokens;
namespace Bunny.Common.Utils.Net;
public static partial class NetUtil
{
public static List<string> Ignores = ["/api/Login"];
/// <summary>
/// 当前路径是否匹配ant path路径
/// </summary>
/// <param name="pattern">ant path值</param>
/// <param name="path">当前路径</param>
/// <returns>是否匹配</returns>
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);
}
/// <summary>
/// 是否匹配当前路径,同时判断是否在忽略列表中
/// 如果在忽略列表中,则不进行判断
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool MatchPath(string? path)
{
// 如果当前路径是在忽略列表中,返回未匹配
return !Ignores.Select(ignore => AntPath(ignore, path)).Any(isMatch => isMatch);
}
}