今天我遇到了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;
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 ==用戶)
該問題的任何想法?
謝謝您的回答!
的Gabor
在用戶模型中,您使用'$ this-> auth_key'和'$ this-> auth_Key'(uppe rcase K)。是原因嗎? – rkm