当前位置: 首页 > wzjs >正文

怎么样自己做一个网站爱站权重

怎么样自己做一个网站,爱站权重,有人模仿qq音乐做的h5网站吗,湖南省建设厅领导名单在 .NET 中,为解决接口防暴力调用问题,可通过限制请求频率实现。下面给出几种不同实现方式。 基于内存的简单速率限制 此方法适用于单服务器环境,它借助内存字典来记录每个客户端的请求次数和时间。 MemoryRateLimitMiddleware.cs using …

在 .NET 中,为解决接口防暴力调用问题,可通过限制请求频率实现。下面给出几种不同实现方式。

基于内存的简单速率限制

此方法适用于单服务器环境,它借助内存字典来记录每个客户端的请求次数和时间。

MemoryRateLimitMiddleware.cs

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;public class MemoryRateLimitMiddleware
{private readonly RequestDelegate _next;private readonly ConcurrentDictionary<string, (int Count, DateTime LastRequestTime)> _requestCounts = new ConcurrentDictionary<string, (int Count, DateTime LastRequestTime)>();private readonly int _maxRequests;private readonly TimeSpan _timeWindow;public MemoryRateLimitMiddleware(RequestDelegate next, int maxRequests, TimeSpan timeWindow){_next = next;_maxRequests = maxRequests;_timeWindow = timeWindow;}public async Task InvokeAsync(HttpContext context){var clientIp = context.Connection.RemoteIpAddress.ToString();var now = DateTime.UtcNow;if (_requestCounts.TryGetValue(clientIp, out var entry)){if (now - entry.LastRequestTime < _timeWindow){if (entry.Count >= _maxRequests){context.Response.StatusCode = 429;await context.Response.WriteAsync("请求频率过高,请稍后再试。");return;}_requestCounts[clientIp] = (entry.Count + 1, entry.LastRequestTime);}else{_requestCounts[clientIp] = (1, now);}}else{_requestCounts.TryAdd(clientIp, (1, now));}await _next(context);}
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;public class Startup
{public void ConfigureServices(IServiceCollection services){services.AddControllers();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseMiddleware<MemoryRateLimitMiddleware>(100, TimeSpan.FromMinutes(1));app.UseEndpoints(endpoints =>{endpoints.MapControllers();});}
}

基于 Redis 的分布式速率限制

若应用部署在分布式环境,可使用 Redis 存储请求信息。需安装 StackExchange.Redis 包。

RedisRateLimitMiddleware.cs

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using StackExchange.Redis;public class RedisRateLimitMiddleware
{private readonly RequestDelegate _next;private readonly IDatabase _redisDb;private readonly int _maxRequests;private readonly TimeSpan _timeWindow;public RedisRateLimitMiddleware(RequestDelegate next, ConnectionMultiplexer redis, int maxRequests, TimeSpan timeWindow){_next = next;_redisDb = redis.GetDatabase();_maxRequests = maxRequests;_timeWindow = timeWindow;}public async Task InvokeAsync(HttpContext context){var clientIp = context.Connection.RemoteIpAddress.ToString();var key = $"request_count:{clientIp}";var count = await _redisDb.StringGetAsync(key);if (count.HasValue){var currentCount = int.Parse(count);if (currentCount >= _maxRequests){context.Response.StatusCode = 429;await context.Response.WriteAsync("请求频率过高,请稍后再试。");return;}await _redisDb.StringIncrementAsync(key);}else{await _redisDb.StringSetAsync(key, 1, _timeWindow);}await _next(context);}
}

StartupRedis.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StackExchange.Redis;public class StartupRedis
{public void ConfigureServices(IServiceCollection services){var redis = ConnectionMultiplexer.Connect("localhost");services.AddSingleton<IConnectionMultiplexer>(redis);services.AddControllers();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConnectionMultiplexer redis){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseMiddleware<RedisRateLimitMiddleware>(redis, 100, TimeSpan.FromMinutes(1));app.UseEndpoints(endpoints =>{endpoints.MapControllers();});}
}

解释

  • 基于内存的简单速率限制:借助 ConcurrentDictionary 记录每个客户端的请求次数和时间,当请求频率超出限制时,返回 429 状态码。
  • 基于 Redis 的分布式速率限制:使用 Redis 存储请求信息,能处理分布式环境下的请求频率限制。
http://www.dtcms.com/wzjs/603387.html

相关文章:

  • 网站后台怎么更新html5做静态网站
  • 网站的内链怎么做建站之星做出的网站不安全
  • 为什么网站开发需要写php软件如何制作
  • 网站剪辑培训机构排名WordPress全功能高级投稿
  • 查看网站 vps网站建设忄金手指专业
  • 做co网站网页设计教学网站
  • 地下城做心悦任务的网站电子商务网站建设报告怎么写
  • 西安市城乡建设厅网站四川手机网站设计方案
  • 国外设计网站app网页小游戏在线玩无需登录
  • 天津平台网站建设公司网站推广的方法ppt
  • 青岛做一个网站多少钱重庆市场调研公司
  • 赣州找工作的网站有限公司章程
  • 国内网站速度慢深圳中光建设有限公司
  • 深圳网站建设小江淄博免费网站建设哪家好
  • 网站维护员是做什么的wordpress 固定导航
  • 电子商务网站建设需要的语言及特点6wordpress mysql权限
  • 0531建设网站昆山品牌网站
  • h5网站搭建线上销售渠道有哪些
  • 志勋网站建设公司哪个公司开发小程序比较好
  • 如何做网站关键词搜索排名优化
  • 做企业云网站的企业金华百度推广公司
  • 网站视频大全湖北网站建设专家
  • 怎么自己制作网站平台重庆建设摩托车股份有限公司官网
  • 自己做文字壁纸的网站前端开发培训机构推荐
  • 四川住房城乡和城乡建设厅网站企业网站建设推广费用
  • 织梦网站备案购物网站建设费用
  • 做个网站得投入多少网站备案包括哪些东西
  • 淘宝美工做倒计时图片网站外贸 wordpress模板下载
  • 电子商务网站开发进什么科目网站本地可以打开
  • 图文网站源码深圳有限公司官网