ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vs2022 搭建NET6 WebApi 接口项目《五》 接口访问限流配置

2022-04-09 07:31:53  阅读:330  来源: 互联网

标签:WebApi AddSingleton IP builder 接口 限流 Services httpContext options


1、在appsetting.json中配置参数

     

 "IpRateLimiting": {
    //false则全局将应用限制,并且仅应用具有作为端点的规则* 。 true则限制将应用于每个端点,如{HTTP_Verb}{PATH}
    "EnableEndpointRateLimiting": true,
    //false则拒绝的API调用不会添加到调用次数计数器上
    "StackBlockedRequests": false,
    //注意这个配置,表示获取用户端的真实IP,我们的线上经过负载后是 X-Forwarded-For,而测试服务器没有,所以是X-Real-IP
    "RealIpHeader": "X-Real-IP",
    "ClientIdHeader": "X-ClientId",
    "HttpStatusCode": 200, //设置返回状态码
    //"QuotaExceededResponse": {
    //  "Content": "{{\"code\":429,\"msg\":\"访问过于频繁,请稍后重试\",\"data\":null}}",
    //  "ContentType": "application/json",
    //  "StatusCode": 200
    //},//返回提示信息
    "IpWhitelist": [], //限制白名单,在名单中的IP,则无访问权限
    "EndpointWhitelist": [],
    "ClientWhitelist": [],
    "GeneralRules": [
      {
        "Endpoint": "*", //对所有接口进行监控
        "Period": "10s",
        "Limit": 5
      }
    ]
  },

2、在program.cs中配置

     

#region 限流配置
//加载配置
builder.Services.AddOptions();
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);//设置兼容性版本
builder.Services.AddMemoryCache();
//加载IpRateLimiting配置
builder.Services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
//注入计数器和规则存储
builder.Services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();
builder.Services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
//添加框架服务
builder.Services.AddMvc();
// clientId / clientIp解析器使用它。
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//配置(计数器密钥生成器)
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
#endregion

3、新建一个IPLimitMiddleware类

    

public class IPLimitMiddleware : IpRateLimitMiddleware
    {
        private readonly IpRateLimitOptions _options;
        private readonly IIpPolicyStore _ipPolicyStore;

        public IPLimitMiddleware(RequestDelegate next,IProcessingStrategy strategy, IOptions<IpRateLimitOptions> options, IIpPolicyStore policyStore, IRateLimitConfiguration config, ILogger<IpRateLimitMiddleware> logger)
            : base(next, strategy, options, policyStore, config, logger)
        {
            _options = options.Value;
            _ipPolicyStore = policyStore;
        }

        public override Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitRule rule, string retryAfter)
        {
            var ip = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = httpContext.Connection.RemoteIpAddress.ToString();
            }

            //后面需要将IP添加到数据库中进行监控,以便对非法IP进行限制

            httpContext.Response.ContentType = "application/json";
            return httpContext.Response.WriteAsync($"{{ \"code\": 429,\"msg\": \"访问过于频繁,请稍后重试\",\"data\":null }}");
            //httpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*");
            //return base.ReturnQuotaExceededResponse(httpContext, rule, retryAfter);
        }
    }

这里不太好测试,就只贴代码

标签:WebApi,AddSingleton,IP,builder,接口,限流,Services,httpContext,options
来源: https://www.cnblogs.com/wenghan/p/16120738.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有