mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Move Throttle filter to docs only
This commit is contained in:
parent
13aa2ab32d
commit
9dc19c3d55
@ -1,46 +0,0 @@
|
||||
<?php namespace App\Filters;
|
||||
|
||||
use CodeIgniter\Filters\FilterInterface;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Config\Services;
|
||||
|
||||
class Throttle implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* This is a demo implementation of using the Throttler class
|
||||
* to implement rate limiting for your application.
|
||||
*
|
||||
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function before(RequestInterface $request)
|
||||
{
|
||||
$throttler = Services::throttler();
|
||||
|
||||
// Restrict an IP address to no more
|
||||
// than 1 request per second across the
|
||||
// entire site.
|
||||
if ($throttler->check($request->getIPAddress(), 60, MINUTE) === false)
|
||||
{
|
||||
return Services::response()->setStatusCode(429);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* We don't have anything to do here.
|
||||
*
|
||||
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
|
||||
* @param ResponseInterface|\CodeIgniter\HTTP\Response $response
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function after(RequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
@ -80,9 +80,9 @@ and you cannot stop script execution. This does allow you to modify the final ou
|
||||
the final output. This could be used to ensure certain security headers were set the correct way, or to cache
|
||||
the final output, or even to filter the final output with a bad words filter.
|
||||
|
||||
===================
|
||||
*******************
|
||||
Configuring Filters
|
||||
===================
|
||||
*******************
|
||||
|
||||
Once you've created your filters, you need to configure when they get run. This is done in ``app/Config/Filters.php``.
|
||||
This file contains four properties that allow you to configure exactly when the filters run.
|
||||
@ -94,7 +94,7 @@ The ``$aliases`` array is used to associate a simple name with one or more fully
|
||||
filters to run::
|
||||
|
||||
public $aliases = [
|
||||
'csrf' => \App\Filters\CSRF::class
|
||||
'csrf' => \CodeIgniter\Filters\CSRF::class
|
||||
];
|
||||
|
||||
Aliases are mandatory and if you try to use a full class name later, the system will throw an error. Defining them
|
||||
@ -181,4 +181,4 @@ a list of URI patterns that filter should apply to::
|
||||
Provided Filters
|
||||
****************
|
||||
|
||||
Three filters are bundled with CodeIgniter4: Honeypot, Security, and Throttler.
|
||||
Three filters are bundled with CodeIgniter4: Honeypot, Security, and DebugToolbar.
|
||||
|
@ -49,20 +49,53 @@ start using it in your application.
|
||||
The Code
|
||||
========
|
||||
|
||||
You can find this file at **app/Filters/Throttle.php** but the relevant method is reproduced here::
|
||||
You could make your own Throttler filter, at **app/Filters/Throttle.php**,
|
||||
along the lines of::
|
||||
|
||||
public function before(RequestInterface $request)
|
||||
{
|
||||
$throttler = Services::throttler();
|
||||
<?php namespace App\Filters;
|
||||
|
||||
// Restrict an IP address to no more
|
||||
// than 1 request per second across the
|
||||
// entire site.
|
||||
if ($throttler->check($request->getIPAddress(), 60, MINUTE) === false)
|
||||
{
|
||||
return Services::response()->setStatusCode(429);
|
||||
}
|
||||
}
|
||||
use CodeIgniter\Filters\FilterInterface;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Config\Services;
|
||||
|
||||
class Throttle implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* This is a demo implementation of using the Throttler class
|
||||
* to implement rate limiting for your application.
|
||||
*
|
||||
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function before(RequestInterface $request)
|
||||
{
|
||||
$throttler = Services::throttler();
|
||||
|
||||
// Restrict an IP address to no more
|
||||
// than 1 request per second across the
|
||||
// entire site.
|
||||
if ($throttler->check($request->getIPAddress(), 60, MINUTE) === false)
|
||||
{
|
||||
return Services::response()->setStatusCode(429);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* We don't have anything to do here.
|
||||
*
|
||||
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
|
||||
* @param ResponseInterface|\CodeIgniter\HTTP\Response $response
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function after(RequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
When run, this method first grabs an instance of the throttler. Next it uses the IP address as the bucket name,
|
||||
and sets things to limit them to one request per second. If the throttler rejects the check, returning false,
|
||||
@ -79,8 +112,7 @@ this to incoming requests, you need to edit **/app/Config/Filters.php** and firs
|
||||
filter::
|
||||
|
||||
public $aliases = [
|
||||
'csrf' => \App\Filters\CSRF::class,
|
||||
'toolbar' => \App\Filters\DebugToolbar::class,
|
||||
...
|
||||
'throttle' => \App\Filters\Throttle::class
|
||||
];
|
||||
|
||||
@ -92,9 +124,9 @@ Next, we assign it to all POST requests made on the site::
|
||||
|
||||
And that's all there is to it. Now all POST requests made on the site will have be rate limited.
|
||||
|
||||
===============
|
||||
***************
|
||||
Class Reference
|
||||
===============
|
||||
***************
|
||||
|
||||
.. php:method:: check(string $key, int $capacity, int $seconds[, int $cost = 1])
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user