JWT Cookie 存储不正确 - Laravel

JWT Cookie is not stored right - Laravel

我正在使用 Laravel 5.1 并且我正在尝试实现基本的 JWT-Auth 逻辑。

虽然我正在创建 jwt_token 权限(我将其记录到 laravel.log,令牌按预期分为三个不同的部分),当我尝试使用jwt_token 值,创建了一个完全不同的 cookie,没有用点分隔,并且比原来的 jwt_token 大得多。

我认为代码非常简单:

$jwt_token = $this->findOrCreateUser($user);
Log::error('My jwt_token is '. $jwt_token); //from the log here i see the right jwt_token

//i disable the http-only flag because i want to read it with js

return redirect('/index.html')->withCookie(Cookie::make('jwt_token',$jwt_token,1000,null,null,false,false));

如果有人能给我一些错误的建议,我将非常高兴!

谢谢

Laravel 5.1 默认附带一个名为 EncryptCookies 的中间件,它在 HTTP 内核的全局 $middleware 数组中注册。因此,cookie 在发送到浏览器时会自动加密,这就是为什么您的 cookie 值不是 JWT 令牌的纯文本表示形式的原因。

您可以通过两种方式轻松解决此问题:

1. 将cookie名称添加到App\Http\Middleware\EncryptCookies:

中的$except数组
protected $except = [
    'jwt_token'
];

2.Comment/removeEncryptCookies中间件在App\Http\Kernelclass(虽然我不推荐这个,除非你特别希望所有 cookie 都未加密):

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,
];