Php Yii2身份验证失败

Php Yii2身份验证失败,php,security,authentication,yii2,Php,Security,Authentication,Yii2,今天我遇到了一个Yii2认证的问题。我成功地实现了,但每次尝试登录时都会显示以下错误: 在我刷新页面1到2次后,错误就会消失,一切正常。我的第一个想法是添加数据库字段auth_key(32)varchar,但它没有解决这个问题 这是我的User.php: <?php namespace app\models; use yii\base\NotSupportedException; use yii\db\ActiveRecord; use yii\helpers\Security; u

今天我遇到了一个Yii2认证的问题。我成功地实现了,但每次尝试登录时都会显示以下错误:

在我刷新页面1到2次后,错误就会消失,一切正常。我的第一个想法是添加数据库字段auth_key(32)varchar,但它没有解决这个问题

这是我的User.php:

<?php

namespace app\models;

use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;


class User extends ActiveRecord implements \yii\web\IdentityInterface
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'felhasznalo';
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['access_token' => $token]);
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByFelhasznalonev($felhasznalonev)
    {
        return static::findOne(['felhasznalonev' => $felhasznalonev]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_Key;
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->auth_Key === $authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->jelszo === sha1($password);
    }
}
以及LoginForm.php:

<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_user = false;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();

            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
        }
        return false;
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByFelhasznalonev($this->username);
        }

        return $this->_user;
    }
}
尝试将LoginForm中的getUser函数更改为:


这只是一个打字错误,您应该使用
auth\u key
而不是
auth\u key

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

好的,我做了更改,但是现在Yii throws Namespace声明语句必须是LoginForm.php中脚本错误的第一条语句声明就在ok之后,这是我的错。。。我在开始标签前有一些空白。对不起,这个愚蠢的问题!不幸的是,这是相同的错误:获取未知属性:app\models\User::auth_key在您使用的用户模型中
$this->auth_key
$this->auth_key
(大写K)。这是原因吗?谢谢,这就是解决办法!我已经找了两天了。
public function getUser()
{
    if ($this->_user === false) {
        $this->_user = User::findByFelhasznalonev($this->username);

        //generate auth_key for a new created User
        $this->_user->generateAuthKey();
    }

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