在两个地方,我发现可以通过设置protected $except
变量来绕过Laravel csrf保护。但它似乎没有根据文件工作:
http://laravel.com/docs/5.1/billing#handling-stripe-webhooks
并在
http://laravel.com/docs/5.1/routing#csrf-protection
protected $except = [
'stripe/*',
];
我正在使用5.1
这是在routes.php
中Route::match(['post'], '/webhooks/provider/callback/{version}', [
'as' => 'provider.webhooks.callback', 'uses' => 'WebhookController@callback'
]);
Route::match(['post'], '/webhooks/provider/fallback/{version}', [
'as' => 'provider.webhooks.fallback', 'uses' => 'WebhookController@fallback'
]);
这是
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier {
protected $except = [
'webhooks/*',
'/webhooks/*',
];
public function handle($request, Closure $next)
{
return parent::handle($request, $next);
}
}
以下是BaseVerifier
我没有看到任何$except
支票的内容:
<?php namespace Illuminate\Foundation\Http\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Session\TokenMismatchException;
use Symfony\Component\Security\Core\Util\StringUtils;
class VerifyCsrfToken implements Middleware {
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->tokensMatch($request))
{
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
}
但是我通过评论解决了问题,但仍然设置$except
应根据文档进行操作;不是吗?:
<?php namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel {
protected $middleware = [
//'App\Http\Middleware\VerifyCsrfToken',
];
}
这是在错误日志中:
[2015-07-06 09:40:34] production.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:46
Stack trace:
#0 /app/Http/Middleware/VerifyCsrfToken.php(26): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
答案 0 :(得分:1)
修改app/Http/Middleware/VerifyCsrfToken.php
//add an array of Routes to skip CSRF check
private $openRoutes = ['free/route', 'free/too'];
//modify this function
public function handle($request, Closure $next)
{
//add this condition
foreach($this->openRoutes as $route) {
if ($request->is($route)) {
return $next($request);
}
}
return parent::handle($request, $next);
}
在$openRoutes
数组中提供您的路线,这将被绕过。
答案 1 :(得分:1)
因此,对于Laravel 5.0,你可以使用它;
private $openRoutes = ['webhooks/free', 'webhooks/*'];
public function handle($request, Closure $next)
{
if(in_array($request->path(), $this->openRoutes)){
return $next($request);
}
return parent::handle($request, $next);
}
对于Laravel 5.1,您可以使用此功能
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'stripe/*',
];
}
文档来源 http://laravel.com/docs/5.1/routing#csrf-excluding-uris