CakePHP 身份验证插件 returns 空身份对象
CakePHP authentication plugin returns empty identity object
我正在使用 CakePHP 3.5.14 和带有 JWT 身份验证的 official CakePHP authentication plugin v1.0.0-rc4。身份验证本身(生成和评估令牌)有效,但试图获取令牌持有者的身份 returns 一个空对象。
我设置了 User
模型 (which is the default model for the ORM resolver) 和 运行,users
table 没有什么特别之处,它有通常的列 [=15] =]、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;
一个空的 JSON 表示并不一定意味着该对象是 "empty",而只是它的 JSON 表示是。
围绕身份的 API 最近发生了很大变化,\Authentication\Controller\Component\AuthenticationComponent::getIdentity()
returns \Authentication\IdentityInterface
,不包含 JSON 序列化功能,并且可用的 IdentityInterface
实现不会将数据存储在 public 属性中,因此其 JSON 表示将始终为空。
如果你想访问检索到的数据,对于 ORM 解析器来说,它应该是一个实体(请注意,合同定义了 \ArrayAccess|array
,所以如果你需要一个实体,请务必执行检查),然后调用 \Authentication\IdentityInterface::getOriginalData()
:
$identity = $this->Authentication->getIdentity();
$user = $identity->getOriginalData();
另见
我正在使用 CakePHP 3.5.14 和带有 JWT 身份验证的 official CakePHP authentication plugin v1.0.0-rc4。身份验证本身(生成和评估令牌)有效,但试图获取令牌持有者的身份 returns 一个空对象。
我设置了 User
模型 (which is the default model for the ORM resolver) 和 运行,users
table 没有什么特别之处,它有通常的列 [=15] =]、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;
一个空的 JSON 表示并不一定意味着该对象是 "empty",而只是它的 JSON 表示是。
围绕身份的 API 最近发生了很大变化,\Authentication\Controller\Component\AuthenticationComponent::getIdentity()
returns \Authentication\IdentityInterface
,不包含 JSON 序列化功能,并且可用的 IdentityInterface
实现不会将数据存储在 public 属性中,因此其 JSON 表示将始终为空。
如果你想访问检索到的数据,对于 ORM 解析器来说,它应该是一个实体(请注意,合同定义了 \ArrayAccess|array
,所以如果你需要一个实体,请务必执行检查),然后调用 \Authentication\IdentityInterface::getOriginalData()
:
$identity = $this->Authentication->getIdentity();
$user = $identity->getOriginalData();
另见