我正在使用 Laravel 5.1 制作单页应用。我使用 localStorage 来保存 API 密钥,不需要使用 cookies。Laravel 为我创建了两个 cookies:
- 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头部的CSRF令牌。我能完全禁用cookie吗?