网站首页 > 技术教程 正文
使用背景
在达能小程序h5版,小程序的部分接口可以在不用登陆的情况下随意调用。为了防止恶意调用接口服务器带来过大的压力,需要对接口调用次数限制,我使用的限制规则是每个ip每个接口一分钟之内最多访问30次,目前项目是使用的lumen框架开发的接口,这里主要说一下如何在Lumen框架中使用throttle中间件来限制客户端接口请求次数
具体方法
1.新建中间件
在app\Http\Middleware中新建ThrottleRequests.php文件,文件下载地址:https://github.com/illuminate/routing/blob/master/Middleware/ThrottleRequests.php
修改ThrottleRequests.php中的resolveRequestSignature方法用来定义我们自己的限制规则
/**
* Resolve the number of attempts if the user is authenticated or not.
*
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @return int
*/
protected function resolveMaxAttempts($request, $maxAttempts)
{
if (Str::contains($maxAttempts, '|')) {
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
}
if (! is_numeric($maxAttempts) && $request->user()) {
$maxAttempts = $request->user()->{$maxAttempts};
}
return (int) $maxAttempts;
}
2.重新定义异常处理
由于laravel的Throttle在超过频率限制时抛出的异常是一个html页面,但本项目需要以jsonresponse的形式抛给前端,所以需要重新定义异常
在app/Exceptions中新建ThrottleException.php,当访问次数超过限制时抛出异常
<?php
namespace App\Exceptions;
use Exception;
class ThrottleException extends Exception
{
protected $isReport = false;
public function isReport()
{
return $this->isReport;
}
}
在app/Exceptions/Handler.php捕获该抛出异常,在render方法增加以下判断:
//接口请求超出频率限制报错
if ($exception instanceof ThrottleException) {
return response(['code' => $exception->getCode(), 'msg' => $exception->getMessage()], 432);
}
修改ThrottleRequests.php文件中的buildException方法
/**
* Create a 'too many attempts' exception.
*
* @param string $key
* @param int $maxAttempts
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function buildException($key, $maxAttempts)
{
$retryAfter = $this->getTimeUntilNextRetry($key);
$headers = $this->getHeaders(
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
return new ThrottleException('Too Many Attempts.', 432);
}
注册中间件
$app->routeMiddleware([
'admin.auth' => App\Http\Middleware\AdminAuth::class,
'front.auth' => App\Http\Middleware\FrontAuth::class,
'auth' => App\Http\Middleware\Authenticate::class,
'front.checkEnv' => App\Http\Middleware\CheckEnv::class,
'h5.auth' => App\Http\Middleware\H5Auth::class,
'throttle' => App\Http\Middleware\ThrottleRequests::class
]);
4.使用
$router->group(['middleware' => ['throttle:30']], function () use ($router) {
// 登录
$router->post('auth/login', ['uses' => 'UcAuthController@login']);
$router->get('auth/login', ['uses' => 'UcAuthController@login']);
});
总结
限制IP防刷的机制有很多,我们是在业务层通过throttle中间件和通过nginx两方面来限制的。nginx的限制IP访问次数的配置也很简单,具体可参照https://blog.csdn.net/weixin_33755649/article/details/91599721?utm_source=distribute.pc_relevant.none-task来配置
猜你喜欢
- 2024-10-12 打造自动化DOS攻击防御系统:一键屏蔽恶意IP
- 2024-10-12 禁止使用搜索引擎怎么设置(30秒教会你5个小技巧)
- 2024-10-12 apache(nginx)下控制请求频率防IP恶意请求 「二版」
- 2024-10-12 限制IP某个时间段内访问的次数(限制ip怎么解决)
- 2024-10-12 怎么封攻击的IP地址(ip封禁是最简单和有效的防守方式)
- 2024-10-12 Nginx配置限流(nginx 限制流量)
- 2024-10-12 利用openresty+lua+redis 实现封杀频繁恶意访问IP地址
- 2024-10-12 如何用 Nginx 代理 MySQL 连接,并限制可访问 IP?
- 2024-10-12 企业网站维护教程:网站如何禁止国外IP访问?
- 2024-10-12 Linux下WEB中间件禁止IP访问配置方法
你 发表评论:
欢迎- 最近发表
-
- Win10 TH2正式版官方ESD映像转换ISO镜像方法详解
- 使用iso镜像升级到Windows 10的步骤
- macOS Ventura 13.2 (22D49) Boot ISO 原版可引导镜像
- 安利一个用ISO镜像文件制作引导U盘的的小工具RUFUS
- CentOS 7使用ISO镜像配置本地yum源
- 用于x86平台的安卓9.0 ISO镜像发布下载:通吃I/A/N、完全免费
- AlmaLinux 9.6发布:升级工具、初步支持IBM Power虚拟化技术
- Rufus写入工具简洁介绍与教程(写入模式)
- 新硬件也能安装使用了,Edge版Linux Mint 21.3镜像发布
- 开源工程师:Ubuntu应该抛弃32位ISO镜像
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)