【发布时间】:2026-01-15 15:05:01
【问题描述】:
我正在使用带有 JWT 身份验证的 CakePHP 3.5.14 和 official CakePHP authentication plugin v1.0.0-rc4。身份验证本身(生成和评估令牌)有效,但尝试获取令牌持有者的身份会返回一个空对象。
我有我的User 模型(which is the default model for the ORM resolver)设置并运行,users 表没有什么特别之处,它有通常的列id、username、password、email。 cakephp/orm 已被作曲家要求。
这是 JWT 有效载荷的内容:
{
"sub": 9,
"aud": "http://example.com"
}
这是控制器功能的代码:
$this->autoRender = false;
$result = $this->Authentication->getResult();
if ($result->isValid()) { //evaluates true
$user = $this->Authentication->getIdentity();
$response = $user;
} else {
$response = array("error" => "You are not authorized.");
}
$json = json_encode($response); //becomes {}
$this->response->type('json');
$this->response->body($json);
return $this->response;
【问题讨论】:
标签: authentication cakephp jwt cakephp-3.0