Laravel无法使用令牌发送响应

时间:2018-02-21 12:55:38

标签: laravel

我有这段代码

$token = (new Builder())->setIssuer('http://127.0.0.1:8000') // Configures the issuer (iss claim)
     ->setAudience('http://127.0.0.1:8000') // Configures the audience (aud claim)
     ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
     ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
     ->setExpiration(time() + 3600) // Configures the expiration time of the token (exp claim)
     ->set('id', $user->id)
     ->set('name', $user->name)
     ->sign($signer, env('APP_KEY')) // creates a signature using enviroment variable APP_KEY as key
     ->getToken(); // Retrieves the generated token
echo $token . '<br>';
return new Response(['token' => $token], 200);

我正在为用户进行JWT令牌验证。令牌本身很好但不知何故我无法用它做出反应。

SOMETOKENI6IjRmMWcyM2ExMmFhIJpc3MiOiJsadsadadodHRwOlwvXCImh0dHA6XC9cLzEyNy4wLjAuMTo4MDAwIiwianRpIjoiNGYxZzIzYTEyYWEiLCJTc0MjAsImV4cCI6MTUxOTIyMTAyMCwiaWQiOjEsIm5hbWUiOiJ1c2VyIn0.9413Cuf00CKdkDg
{"token":{}}

这是我的控制器代码打印出来的。 它显示当我回显令牌被打印时,但是响应它的空。那是为什么?

2 个答案:

答案 0 :(得分:0)

你可以试试这个

Response::json(array('token'=>$token,'name'=>$name,'status'=>200));

我希望这会有所帮助

答案 1 :(得分:0)

我发现修复对我有用。

return new Response(['token' => "$token"], 200);

不知何故,JWT令牌只能通过使用$token变量添加到数组中。