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

客户端限流主要采用手段:纯前端验证码、禁用按钮、调用限制和假排队

一、纯前端验证码
场景
防止机器人或脚本高频提交,需用户完成验证后才能触发请求。

Vue 前端实现

<template><div><button @click="showCaptcha">提交订单</button><div v-if="captchaVisible"><img :src="captchaImage" alt="验证码" /><input v-model="captchaInput" placeholder="输入验证码" /><button @click="verifyCaptcha">验证</button></div></div>
</template><script>
export default {data() {return {captchaVisible: false,captchaImage: "",captchaInput: "",captchaKey: "", // 后端生成的验证码唯一标识};},methods: {async showCaptcha() {// 向后端请求验证码const res = await this.$axios.get("/api/captcha/generate");this.captchaImage = res.data.image;this.captchaKey = res.data.key;this.captchaVisible = true;},async verifyCaptcha() {// 提交验证码到后端校验const res = await this.$axios.post("/api/captcha/verify", {key: this.captchaKey,code: this.captchaInput,});if (res.data.success) {this.captchaVisible = false;this.submitOrder(); // 验证通过后执行实际提交}},submitOrder() {// 实际业务请求this.$axios.post("/api/order/create");},},
};
</script>

C# 后端实现

[ApiController]
[Route("api/captcha")]
public class CaptchaController : ControllerBase
{private static Dictionary<string, string> _captchas = new Dictionary<string, string>();[HttpGet("generate")]public IActionResult GenerateCaptcha(){// 生成随机验证码(示例简化,实际需生成图片)var code = new Random().Next(1000, 9999).ToString();var key = Guid.NewGuid().ToString();_captchas[key] = code;return Ok(new {key = key,image = $"data:image/png;base64,{GenerateBase64Image(code)}" // 生成图片的Base64});}[HttpPost("verify")]public IActionResult VerifyCaptcha([FromBody] VerifyRequest request){if (_captchas.TryGetValue(request.Key, out var validCode) && validCode == request.Code){_captchas.Remove(request.Key);return Ok(new { success = true });}return Ok(new { success = false });}
}

二、禁用按钮
场景
防止用户重复点击提交按钮,前端临时禁用按钮。

Vue 前端实现

<template><button @click="handleSubmit" :disabled="isSubmitting">{{ isSubmitting ? '提交中...' : '提交订单' }}</button>
</template><script>
export default {data() {return {isSubmitting: false,};},methods: {async handleSubmit() {if (this.isSubmitting) return;this.isSubmitting = true;try {await this.$axios.post("/api/order/create");} finally {this.isSubmitting = false;}},},
};
</script>

三、调用限制(后端频率限制)
场景
限制客户端在固定时间窗口内对同一接口的调用次数。

C# 后端实现(基于内存缓存)

[ApiController]
[Route("api/order")]
public class OrderController : ControllerBase
{private static MemoryCache _requestCache = new MemoryCache(new MemoryCacheOptions());[HttpPost("create")]public IActionResult CreateOrder([FromBody] OrderRequest request){var clientIp = HttpContext.Connection.RemoteIpAddress.ToString();var cacheKey = $"rate_limit_{clientIp}";// 检查请求频率(示例:10秒内最多3次)if (_requestCache.TryGetValue(cacheKey, out int count) && count >= 3){return StatusCode(429, "请求过于频繁,请稍后再试");}// 更新计数器_requestCache.Set(cacheKey, count + 1, TimeSpan.FromSeconds(10));// 实际业务逻辑return Ok(new { success = true });}
}

四、假排队(前端模拟排队)
场景
通过前端动画或提示缓解用户等待焦虑,实际请求仍按正常流程处理。

Vue 前端实现

vue
<template><div><button @click="mockQueue">立即抢购</button><div v-if="isInQueue"><p>排队中,前方还有 {{ queuePosition }} 人...</p><div class="loading-animation"></div></div></div>
</template><script>
export default {data() {return {isInQueue: false,queuePosition: 0,};},methods: {async mockQueue() {if (this.isInQueue) return;this.isInQueue = true;this.queuePosition = Math.floor(Math.random() * 10) + 1; // 模拟随机队列位置// 模拟排队等待(实际请求在后台发送)await new Promise(resolve => setTimeout(resolve, 2000));try {await this.$axios.post("/api/purchase");this.isInQueue = false;} catch (error) {this.isInQueue = false;}},},
};
</script><style>
.loading-animation {width: 20px;height: 20px;border: 2px solid #ccc;border-top-color: #333;border-radius: 50%;animation: spin 1s linear infinite;
}@keyframes spin {to { transform: rotate(360deg); }
}
</style>

总结与对比
在这里插入图片描述

实际建议:

组合使用:验证码 + 后端限流可有效防御自动化攻击。

用户体验优先:禁用按钮和假排队适合提升用户感知。

监控与告警:结合日志监控异常请求模式(如Nginx或ELK)。

http://www.xdnf.cn/news/412597.html

相关文章:

  • jwt学习
  • 如何通过DNS解析实现负载均衡?
  • Android Exoplayer 实现多个音视频文件混合播放以及音轨切换
  • 3d模型的添加与设置
  • VMware虚拟机实例-docker启动失败
  • Linux文件编程——read函数与lseek函数
  • 火狐浏览器安装自定义插件
  • 人工智能的哲学与社会影响
  • 【时时三省】(C语言基础)字符数组的输入输出
  • 做好的QT软件,换一个笔记本打开后发现字体很小,部分字体还被控件遮挡
  • 提示工程实战指南:Google白皮书关键内容一文讲清
  • 第二十二天打卡
  • #将一个 .c 文件转变为可直接运行的文件过程及原理
  • CTF实战秘籍:跨平台文件合并与数据重构技术
  • linux-进程信号的产生
  • OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
  • 嵌入式MCU和Linux开发哪个好?
  • FreeRTOS的学习记录(基础知识)
  • FPGA----petalinux开机启动自定义脚本/程序的保姆级教程(二)
  • 【超详细教程】安卓模拟器如何添加本地文件?音乐/照片/视频一键导入!
  • 利用基于LLM的概念提取和FakeCTI数据集提升网络威胁情报对抗虚假信息活动的能力
  • 区块链+农业:从田间到餐桌的信任革命
  • Ref是什么
  • 洛谷 P1082:[NOIP 2012 提高组] 同余方程 ← 求逆元
  • 代码随想录训练营第二十二天| 101.对称二叉树 100.相同的树
  • 综合实验二之grub2密文加密
  • (leetcode) 力扣100 10.和为K的子数组(前缀和+哈希)
  • 【Bootstrap V4系列】学习入门教程之 组件-模态框(Modal)
  • css 点击后改变样式
  • Megatron系列——张量并行