当前位置:网站首页>Thinkphp6.0 middleware with limited access frequency think throttle
Thinkphp6.0 middleware with limited access frequency think throttle
2022-07-04 05:29:00 【Yuan rises and falls】
effect
The middleware can limit the number of visits of users in a period of time , It can be used to protect the interface from climbing and explosion .
install
composer require topthink/think-throttle
After installation, the project will be automatically generated conf/throttle.php
The configuration file , Components are not automatically enabled after installation , Manual setting required .
Turn on
Components work as middleware , Therefore, its startup is the same as other middleware , For example, in global middleware app/middleware.php
:
<?php
return [
\think\middleware\Throttle::class,
];
Configuration instructions
stay config/throttle.php
configuration option :
<?php
// Middleware configuration
return [
// Cache key prefix , Prevent key values from conflicting with other applications
'prefix' => 'throttle_',
// The cache keys ,true Indicates the source of use ip
'key' => true,
// The type of request to be restricted , eg: GET POST PUT DELETE HEAD
'visit_method' => ['GET'],
// Set access frequency , for example '10/m' It means allowing requests per minute 10 Time . value null Means unrestricted , eg: null 10/m 20/h 300/d 200/300
'visit_rate' => '100/m',
// Response returned when access is restricted
'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds) {
return Response::create('Too many requests, try again after ' . $wait_seconds . ' seconds.')->code(429);
},
];
When the configuration item meets any of the following conditions , Access frequency will not be limited :
key
The value isfalse
ornull
;visit_rate
The value isnull
.
among key
The key used to set the cache ; and visit_rate
Used to set the access frequency , The unit can be seconds , branch , when , God , for example :1/s
, 10/m
, 98/h
, 100/d
, It can also be 100/600
(600 Maximum in seconds 100 Requests ).
Flexible customization
Example 1 : Make restrictions on individual users , key
The value of can be set as a function , This function returns the new cache key value ( need Session Support ), for example :
'key' => function($throttle, $request) {
$user_id = $request->session->get('user_id');
return $user_id ;
},
Example 2 : You can also customize the generation for different controllers and methods in the callback function key, The middleware will transform :
'key' => function($throttle, $request) {
return '__CONTROLLER__/__ACTION__/__IP__';
},
Or set it directly :
'key' => '__CONTROLLER__/__ACTION__/__IP__',
PS: This example requires the middleware to be enabled after routing the middleware , In this way, the preset replacement function will take effect .
Example 3 : It is allowed to modify the current access frequency or temporarily change the current limiting policy within the closure :
'key' => function($throttle, $request) {
$throttle->setRate('5/m'); // Set the frequency
$throttle->setDriverClass(CounterSlider::class);// Set current limiting policy
return true;
},
Example 4 : Allow independent configuration in route definition (1.3.x Versioning support )
Route::group(function() {
// Route registration
})->middleware(\think\middleware\Throttle::class, [
'visit_rate' => '20/m',
'key' => '__CONTROLLER__/__ACTION__/__IP__',
]);
边栏推荐
- ansys命令
- 简易零钱通
- LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
- Trie number dictionary tree
- Graduation design of small programs -- small programs of food and recipes
- 【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
- Principle and practice of common defects in RSA encryption application
- [matlab] matlab simulation modulation system FM system
- Automated testing selenium foundation -- webdriverapi
- [matlab] matlab simulation modulation system SSB system
猜你喜欢
Appearance of LabVIEW error dialog box
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
BUU-Crypto-Cipher
空洞卷积、可变形卷积、可变形ROI Pooling
VB. Net simple processing pictures, black and white (class library - 7)
云原生架构实战案例及优化解决方案
LabVIEW错误对话框的出现
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
小程序毕业设计---美食、菜谱小程序
随机推荐
(4) Canal multi instance use
left_and_right_net正常版本
JS string splicing
C # character similarity comparison general class
VB. Net simple processing pictures, black and white (class library - 7)
Topological sorting and graphical display of critical path
[paper summary] zero shot semantic segmentation
[matlab] general function of communication signal modulation Fourier transform
Redis realizes ranking function
光模塊字母含義及參數簡稱大全
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
JS string splicing enhancement
Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
Trie number dictionary tree
Remote desktop client RDP
[matlab] general function of communication signal modulation inverse Fourier transform
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
Zhongke Panyun - data analysis and forensics packet flag
企业级日志分析系统ELK(如果事与愿违那一定另有安排)