用操作筛选器实现请求限流器 filter
我们在操作筛选器中不仅可以在操作方法之前或者之后添加代码,还可以在满足条件的时候终止操作方法的执行。
为了避免恶意客户端频繁发送大量请求而消耗服务器资源,我们要实现 1 秒内只允许同一个IP访问一次请求。
/// <summary>
/// 请求限流器,1秒内同一个IP只允许访问一次
/// </summary>
public class RateLimitFilter : IAsyncActionFilter
{
private readonly IMemoryCache memCache;
public RateLimitFilter(IMemoryCache memCache)
{
this.memCache = memCache;
}
public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
string removeIP = context.HttpContext.Connection.RemoteIpAddress.ToString();
//string removeIP = "192.168.0.8";//测试
string cacheKey = $"LastVisitTick_{removeIP}";
long? lastTick = memCache.Get<long>(cacheKey);
if (lastTick == null || Environment.TickCount64 - lastTick > 1000)
{//首次访问或访问间隔超过1秒,则更新缓存时间
memCache.Set(cacheKey, Environment.TickCount64, TimeSpan.FromSeconds(10)); ;
return next();
}
else
{//不调用next()方法,即终止操作执行,被调用的方法不再执行。
context.Result = new ContentResult { StatusCode = 429 };
return Task.CompletedTask;
}
}
}
注册服务
builder.Services.Configure<MvcOptions>(options =>
{
options.Filters.Add<RateLimitFilter>();
});
builder.Services.AddMemoryCache();
浙公网安备 33010602011771号