2015-11-20 54 views
1

今天我遇到了Yii2身份验证问题。我成功地实现,但是当我尝试登录,每次它让我看到以下错误:Yii2身份验证失败

enter image description here

后我刷新页面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); 
    } 
} 

登录操作:

public function actionLogin() 
    { 

     if (!\Yii::$app->user->isGuest) { 
      if (empty($_SESSION['ablak_id'])) { 
       $_SESSION['ablak_id'] = Yii::$app->request->post('a_id'); 
      } 
      else { 
       return $this->redirect(Url::to(['ugyfelhivo/muszerfal/' . $_SESSION['ablak_id']])); 
      } 
     } 

     $model = new LoginForm(); 
     if ($model->load(Yii::$app->request->post()) && $model->login()) { 
      $session = Yii::$app->session; 
      $session->set('ablak_id', Yii::$app->request->post('ablak_id')); 
      return $this->redirect(Url::to(['ugyfelhivo/muszerfal/' . $_SESSION['ablak_id']])); 
     } 

     //Lekérdezzük az elérhető rendelők nevét majde elküldjük kimenetre 
     $ablakok = Ablak::find()->all(); 

     return $this->render('login', [ 
      'model' => $model, 
      'ablakok' => $ablakok, 
     ]); 
    } 

而且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; 
    } 
} 

这是为用户的表结构表(匈牙利felhasznalok ==用户)

enter image description here

该问题的任何想法?

谢谢您的回答!

的Gabor

+0

在用户模型中,您使用'$ this-> auth_key'和'$ this-> auth_Key'(uppe rcase K)。是原因吗? – rkm

回答

1

它只是一个错字,你应该使用auth_key而不是auth_Key

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

谢谢,这是解决方案!我现在正在寻找那个女士K 2天。 – Gabesz

1

尝试改变的getUser功能在您LoginForm的到:

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; 
} 
+0

好吧,我做了更改,但现在Yii抛出**命名空间声明语句必须是脚本**中的第一个语句** LoginForm.php中的错误** 该声明紧跟在<?php标记之后。 – Gabesz

+0

好吧,这是我的错......我在开标签前留了一些空格。对不起,这个愚蠢的问题! – Gabesz

+0

不幸的是它是相同的错误:获取未知属性:app \ models \ User :: auth_Key – Gabesz