[英]Yii2 Authentication fails
今天,我在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
嘗試將LoginForm中的getUser函數更改為:
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;
}
這只是一個錯字,您應該使用auth_key
而不是auth_Key
:
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.