大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用Asp.NET怎么实现一个限流控制功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
专注于为中小企业提供成都网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业赤峰林西免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。AspNetCoreRateLimit 是一个ASP.NET Core速率限制的解决方案,旨在控制客户端根据IP地址或客户端ID向Web API或MVC应用发出的请求的速率。AspNetCoreRateLimit包含一个IpRateLimitMiddleware 和ClientRateLimitMiddleware ,每个中间件可以根据不同的场景配置限制允许IP或客户端,自定义这些限制策略,也可以将限制策略应用在每个API URL或具体的HTTP Method上。
由上面介绍可知AspNetCoreRateLimit支持了两种方式:基于 客户端IP(IpRateLimitMiddleware) 和客户端ID(ClientRateLimitMiddleware )速率限制 接下来就分别说明使用方式
添加Nuget包引用:
Install-Package AspNetCoreRateLimit
1、修改Startup.cs中方法:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //需要从加载配置文件appsettings.json services.AddOptions(); //需要存储速率限制计算器和ip规则 services.AddMemoryCache(); //从appsettings.json中加载常规配置,IpRateLimiting与配置文件中节点对应 services.Configure(Configuration.GetSection("IpRateLimiting")); //从appsettings.json中加载Ip规则 services.Configure (Configuration.GetSection("IpRateLimitPolicies")); //注入计数器和规则存储 services.AddSingleton (); services.AddSingleton (); services.AddControllers(); services.AddSingleton (); //配置(解析器、计数器密钥生成器) services.AddSingleton (); //Other Code } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //Other Code app.UseRouting(); app.UseAuthorization(); //启用客户端IP限制速率 app.UseIpRateLimiting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
2、在appsettings.json中添加通用配置项节点:(IpRateLimiting节点与Startup中取的节点对应)
"IpRateLimiting": { //false,则全局将应用限制,并且仅应用具有作为端点的规则*。例如,如果您设置每秒5次调用的限制,则对任何端点的任何HTTP调用都将计入该限制 //true, 则限制将应用于每个端点,如{HTTP_Verb}{PATH}。例如,如果您为*:/api/values客户端设置每秒5个呼叫的限制, "EnableEndpointRateLimiting": false, //false,拒绝的API调用不会添加到调用次数计数器上;如 客户端每秒发出3个请求并且您设置了每秒一个调用的限制,则每分钟或每天计数器等其他限制将仅记录第一个调用,即成功的API调用。如果您希望被拒绝的API调用计入其他时间的显示(分钟,小时等) //,则必须设置StackBlockedRequests为true。 "StackBlockedRequests": false, //Kestrel 服务器背后是一个反向代理,如果你的代理服务器使用不同的页眉然后提取客户端IP X-Real-IP使用此选项来设置 "RealIpHeader": "X-Real-IP", //取白名单的客户端ID。如果此标头中存在客户端ID并且与ClientWhitelist中指定的值匹配,则不应用速率限制。 "ClientIdHeader": "X-ClientId", //限制状态码 "HttpStatusCode": 429, ////IP白名单:支持Ip v4和v6 //"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ], ////端点白名单 //"EndpointWhitelist": [ "get:/api/license", "*:/api/status" ], ////客户端白名单 //"ClientWhitelist": [ "dev-id-1", "dev-id-2" ], //通用规则 "GeneralRules": [ { //端点路径 "Endpoint": "*", //时间段,格式:{数字}{单位};可使用单位:s, m, h, d "Period": "1s", //限制 "Limit": 2 }, //15分钟只能调用100次 {"Endpoint": "*","Period": "15m","Limit": 100}, //12H只能调用1000 {"Endpoint": "*","Period": "12h","Limit": 1000}, //7天只能调用10000次 {"Endpoint": "*","Period": "7d","Limit": 10000} ] }
配置节点已添加相应注释信息。
规则设置格式:
端点格式:{HTTP_Verb}:{PATH}
,您可以使用asterix符号来定位任何HTTP谓词。
期间格式:{INT}{PERIOD_TYPE}
,您可以使用以下期间类型之一:s, m, h, d
。
限制格式:{LONG}
3、特点Ip限制规则设置,在appsettings.json中添加 IP规则配置节点
"IpRateLimitPolicies": { //ip规则 "IpRules": [ { //IP "Ip": "84.247.85.224", //规则内容 "Rules": [ //1s请求10次 {"Endpoint": "*","Period": "1s","Limit": 10}, //15分钟请求200次 {"Endpoint": "*","Period": "15m","Limit": 200} ] }, { //ip支持设置多个 "Ip": "192.168.3.22/25", "Rules": [ //1秒请求5次 {"Endpoint": "*","Period": "1s","Limit": 5}, //15分钟请求150次 {"Endpoint": "*","Period": "15m","Limit": 150}, //12小时请求500次 {"Endpoint": "*","Period": "12h","Limit": 500} ] } ] }
1、修改Startup文件:
public void ConfigureServices(IServiceCollection services) { //需要从加载配置文件appsettings.json services.AddOptions(); //需要存储速率限制计算器和ip规则 services.AddMemoryCache(); //从appsettings.json中加载常规配置 services.Configure(Configuration.GetSection("IPRateLimiting")); //从appsettings.json中加载客户端规则 services.Configure (Configuration.GetSection("ClientRateLimitPolicies")); //注入计数器和规则存储 services.AddSingleton (); services.AddSingleton (); services.AddControllers(); // https://github.com/aspnet/Hosting/issues/793 // the IHttpContextAccessor service is not registered by default. //注入计数器和规则存储 services.AddSingleton (); //配置(解析器、计数器密钥生成器) services.AddSingleton (); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //启用客户端限制 app.UseClientRateLimiting(); app.UseMvc(); }
2、通用配置采用IP限制相同配置,添加客户端限制配置:
//客户端限制设置 "ClientRateLimitPolicies": { "ClientRules": [ { //客户端id "ClientId": "client-id-1", "Rules": [ {"Endpoint": "*","Period": "1s","Limit": 10}, {"Endpoint": "*","Period": "15m","Limit": 200} ] }, { "ClientId": "client-id-2", "Rules": [ {"Endpoint": "*","Period": "1s","Limit": 5}, {"Endpoint": "*","Period": "15m","Limit": 150}, {"Endpoint": "*","Period": "12h","Limit": 500} ] } ] }
3、调用结果:
设置规则:1s只能调用一次:首次调用
调用第二次:自定义返回内容
添加 IpRateLimitController控制器:
////// IP限制控制器 /// [Route("api/[controller]")] [ApiController] public class IpRateLimitController : ControllerBase { private readonly IpRateLimitOptions _options; private readonly IIpPolicyStore _ipPolicyStore; ////// /// /// /// public IpRateLimitController(IOptionsoptionsAccessor, IIpPolicyStore ipPolicyStore) { _options = optionsAccessor.Value; _ipPolicyStore = ipPolicyStore; } /// /// 获取限制规则 /// ///[HttpGet] public async Task Get() { return await _ipPolicyStore.GetAsync(_options.IpPolicyPrefix); } /// /// /// [HttpPost] public async Task Post(IpRateLimitPolicy ipRate) { var pol = await _ipPolicyStore.GetAsync(_options.IpPolicyPrefix); if (ipRate != null) { pol.IpRules.Add(ipRate); await _ipPolicyStore.SetAsync(_options.IpPolicyPrefix, pol); } } }
分布式部署时,需要将速率限制计算器和ip规则存储到分布式缓存中如Redis
修改注入对象
// inject counter and rules distributed cache stores services.AddSingleton(); services.AddSingleton ();
添加Nuget包 Microsoft.Extensions.Caching.StackExchangeRedis
在Startup中设置Redis连接
services.AddStackExchangeRedisCache(options => { options.ConfigurationOptions = new ConfigurationOptions { //silently retry in the background if the Redis connection is temporarily down AbortOnConnectFail = false }; options.Configuration = "localhost:6379"; options.InstanceName = "AspNetRateLimit"; });
限制时自定义相应结果:
//请求返回 "QuotaExceededResponse": { "Content": "{{\"code\":429,\"msg\":\"Visit too frequently, please try again later\",\"data\":null}}", "ContentType": "application/json;utf-8", "StatusCode": 429 },
调用时返回结果:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。