oscarotero / psr7-middlewares

[DEPRECATED] Collection of PSR-7 middlewares
MIT License
668 stars 56 forks source link

Firewall middleware needs ClientIp executed before #89

Closed babeuloula closed 6 years ago

babeuloula commented 6 years ago

Hello,

I use Slim3 and I try to use the firewall but I've an error :

use Psr7Middlewares\Middleware;

// Firewall
$app->add(Middleware::ClientIp());
$app->add(Middleware::Firewall(['37.97.90.193']));

ClientIp has launch before Firewall. Why I've this error ?

Thanks

oscarotero commented 6 years ago

What error is throwed?

babeuloula commented 6 years ago

This is the subjet of the topic : Firewall middleware needs ClientIp executed before

File: .../psr7-middlewares/src/Middleware/Firewall.php:76

oscarotero commented 6 years ago

I don't see why are you getting this error, but suspect it's something unrelated with this package. Are you sue that ClientIp is executed before Firewall, and the request passed to Firewall has the request attribute with the ClientIp data?

babeuloula commented 6 years ago

Of course ClientIp is launch before. As you can see I execute ClientIp before Firewall :

$app->add(Middleware::ClientIp());
$app->add(Middleware::Firewall(['37.97.90.193']));

If you read the documentation of Slim3, you register middleware like this $app->add( new ExampleMiddleware());

oscarotero commented 6 years ago

I just installed and try slim and works fine. The problem is you're adding the middlewares in reverse order. Slim insert the middlewares at the begining of the stack so in your example firewall is executed before clientIp.

The opposite order should work fine:

$app->add(Middleware::Firewall(['37.97.90.193']));
$app->add(Middleware::ClientIp());
danopz commented 6 years ago

@babeuloula this behavior is descibed here: https://www.slimframework.com/docs/concepts/middleware.html#how-does-middleware-work

babeuloula commented 6 years ago

@oscarotero Ok I will test this tomorrow. Thanks