我正在使用 Laravel 5.1 制作单页应用程序。我使用 localStorage 来保存 API key ,我不需要 cookie。 Laravel 为我创建了两个 cookie:
- XSRF- token
- laravel_session
如果我在环境配置中将 SESSION_DRIVER
设置为 array
,则不再生成 laravel_session
cookie。
但我认为XSRF-TOKEN
cookie可能有问题,因为我在VerifyCsrfToken
中间件类中找到了这段代码:
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->shouldPassThrough($request) || $this->tokensMatch($request)) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
addCookieToResponse
方法如下所示:
protected function addCookieToResponse($request, $response)
{
$config = config('session');
$response->headers->setCookie(
new Cookie(
'XSRF-TOKEN', $request->session()->token(), time() + 60 * 120,
$config['path'], $config['domain'], false, false
)
);
return $response;
}
它似乎无论如何都会设置这个 cookie。我可以禁用这个中间件,但我想用它来验证带有 HTTP header 的 CSRF token 。我可以完全禁用 cookie 吗?
最佳答案
只需注释掉 app\Http\Kernel.php
中与 cookie 和 session 相关的行。以下是我发现的;
\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,
希望对您有所帮助。
关于php - 如何在 Laravel 5 中禁用 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964610/