请帮我演示如何在laravel 5中为build api rest禁用csrf unprotect方法(POST,PUT和DELETE)谢谢
答案 0 :(得分:1)
转到app-> http->内核
打开内核文件:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
// \App\Http\Middleware\VerifyCsrfToken::class, // this is the csrf token, just disable using '//'
];
答案 1 :(得分:0)
这是here的方法,我测试过它应该没问题。
简而言之,要在特定页面中禁用csrf,只需将app / Http / Middleware / VerifyCsrfToken.php更改为以下内容:
public function handle($request, Closure $next)
{
//disable CSRF check on following routes
$skip = array(
'user/path/xys',
'user/profile',
'my/unprotected/route'
);
foreach ($skip as $key => $route) {
//skip csrf check on route
if($request->is($route)){
return parent::addCookieToResponse($request, $next($request));
}
}
return parent::handle($request, $next);
}
答案 2 :(得分:0)
如果您只关注/api/*
路线,可以在Stack Overflow
HOpe这可以帮助您获得干净和简短的代码。