yii2 的 auth_key 不会干扰 cookie 库

the auth_key of yii2 is'nt interference in cookie base

我在使用 auth_key 时遇到问题,我有登录表单并且它在没有记住我和记住我的情况下都能正常工作,但我阅读了 yii 文档,在该文档中写了关于记住我使用 id 和 auth_key 用于创建 cookie 以长时间保留用户,我检查了框架代码,其中有三个参数(id、auth_key、expire_time()),我将 auth_key 保存在用户中table 代码在这里

public function generateAuthKey()
{
    $this->auth_key = Yii::$app->security->generateRandomString();
}

public function validateAuthKey($authKey)
{
    return $this->getAuthKey() === $authKey;
}

public function getAuthKey()
{
    return $this->auth_key;
}

但我有问题,如果用户登录网站,我转到用户 table 并更改 auth_key 字段,现在如果用户刷新页面,它必须被丢弃该网站因为它的授权密钥已更改,但用户仍登录该网站,问题出在哪里?

auth_key的主要用途是通过cookie对用户进行认证(用户不用再输入登录数据)。当您选择在登录时被记住时,这就是您被记住的方式。系统必须以某种方式识别并登录您。如果您更改它,它不会注销用户。

您可以尝试在"ValidateAutney"方法中自行更改密钥,但这将是一个不好的做法,最好设置会话时间。

'session' => [
        'class' => 'yii\mongodb\Session',
        'writeCallback' => function($session)
        {
            return [
                'user_id' => Yii::$app->user->id,
                'agent' => Yii::$app->request->getUserAgent(),
                'ip' => Yii::$app->request->getUserIP(),
                'auth_key' => Yii::$app->security->generateRandomString(),
            ];
        }
    ],

    public function getAuthKey()
{

    Yii::$app->session->open();

    $query = new Query();

    $query->select(['auth_key'])
        ->from('cache')
        ->where(['id'=> Yii::$app->session->id ]);
    $row = $query->one();
    return $row['auth_key'];
}