php - yii2 身份 $authKey 返回数组
问题描述
我正在使用yii2
. 当用户通过身份签出登录时,我自己创建一个令牌,自己将其写入 cookie 和数据库中。因此,一个用户可以从多个设备登录,单个用户可以在数据库中拥有多个令牌。
当我很长时间没有访问该站点时,会话和cookies死亡并出现错误,将$ authKey数组转换为字符串。它发生在课堂yii \ web \ User
上。
protected function getIdentityAndDurationFromCookie(){
$value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']);
if ($value === null) {
return null;
}
$data = json_decode($value, true);
if (is_array($data) && count($data) == 3) {
list($id, $authKey, $duration) = $data;
/* @var $class IdentityInterface */
$class = $this->identityClass;
$identity = $class::findIdentity($id);
if ($identity !== null) {
if (!$identity instanceof IdentityInterface) {
throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface.");
} elseif (!$identity->validateAuthKey($authKey)) {
ERROR HERE: Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__);
} else {
return ['identity' => $identity, 'duration' => $duration];
}
}
}
$this->removeIdentityCookie();
return null;
}
我在 Models\User 中的 validateAuthKey
public function validateAuthKey($authKey)
{
$tokenArray = $this->getAuthKey();
foreach($tokenArray as $item){
if ($item->Token === $authKey){
return true;
}
}
return false;
}
在此方法中,从数据库中获取该用户的所有令牌并搜索匹配项
我显示$authKey
并yii\web\User
得到了一个难以理解的数组。数组有 30 个空对象。为什么$ authKey 是由数组组成的?
据我了解,它应该只包含我需要的 1 个令牌,我做错了什么?
解决方案
推荐阅读
- python - 迭代列表的多个变量
- amazon-web-services - 使用 aws 的 DataSync 将文件从 s3 同步到本地机器时,上传期间指定的元数据 (x-amz-mata-metada) S3 文件丢失
- java - Chrome 浏览器中的 Selenium 在线程“main”org.openqa.selenium.WebDriverException 中显示异常:等待驱动程序服务器启动时超时
- azure - MyApps 门户中的 Azure AD 应用注册
- asp.net - 发布 ASP.net 项目时出错,如何解决?
- elasticsearch - 如何防止 ilm 策略执行期间的数据丢失 - 弹性搜索中的翻转、删除?
- python - 为什么请求会引发此异常“check_hostname 需要 server_hostname”?
- angular - Angular 为 Get Request、cURL、Python 和 fetch 工作生成 429 状态代码就好了
- prestashop - Prestashop 将购物车内容带到首页
- postgresql - 使用 JDBCTemplate 运行 sql 函数