Redirect yii2在登录重定向后丢失用户身份

Redirect yii2在登录重定向后丢失用户身份,redirect,identity,yii2,Redirect,Identity,Yii2,我已经研究了其他类似的问题,虽然这个()问了几乎相同的问题,但没有适用于我的情况的解决方案 我创建了一个新的identity类,实现了所有必要的方法并实现了IdentityInterface。请注意,它并没有扩展ActiveRecord,而是扩展了我自己的不从AR派生的基本模型类 一切似乎都很好,我可以登录并正确验证。我已经跟踪了代码,在调用IdentityInterface::validatePassword()和user::login()之后,我可以看到在Yii::$app->user中正确

我已经研究了其他类似的问题,虽然这个()问了几乎相同的问题,但没有适用于我的情况的解决方案

我创建了一个新的identity类,实现了所有必要的方法并实现了IdentityInterface。请注意,它并没有扩展ActiveRecord,而是扩展了我自己的不从AR派生的基本模型类

一切似乎都很好,我可以登录并正确验证。我已经跟踪了代码,在调用IdentityInterface::validatePassword()和user::login()之后,我可以看到在Yii::$app->user中正确设置了标识类

但是,一旦登录成功并且用户被重定向,Yii::$app->user将包含一个空的IdentityInterface,而不是重定向之前的IdentityInterface

在SiteController::actionLogin()中,我修改了以下代码:

return $this->goHome();
致:

$tmp=$this->goHome();
//回声';变量转储(Yii::$app->user);回声';出口
回报(tmp);
我可以看到Yii::$app->user在return()语句之前仍然具有正确的标识

谁能告诉我为什么我会失去我的身份?我已经找到了我能想到的所有东西:yii\web\Controller、yii\base\Controller、yii\web\Response、SiteController、yii\web\User等等


任何帮助都将不胜感激。谢谢

我希望这能对你有所帮助

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

private $_user = false;
public $rememberMe = true;
/*
 * static variable
 */
private static $users = [];

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

/**
 * @inheritdoc
 */
public function rules() {
    return [
        [['username', 'email', 'password', 'loginType'], 'required'],

    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels() {
    return [
        'id' => Yii::t('app', 'ID'),
        'username' => Yii::t('app', 'Username'),

        'email' => Yii::t('app', 'Email'),
        'password' => Yii::t('app', '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);
    } else {
        return false;
    }
}

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

    return $this->_user;
}

public function validatePassword($password) {
    if ($this->password == md5($password)) {
        return true;
    }
    return false;
}

public static function findIdentity($id) {
    return static::findOne($id);
}

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

public function getId() {
    return $this->id;
}

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

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

public static function findByUsername($username) {
    /*
     * Get Record from Usertable
     */
    $user = User::findOne(["username" => $username]);
    if (!empty($user)) {
        /* Create array with your active record like below */
        $identity_use = ['id' => $user['id'],
            'username' => $user['username'],
            'password' => $user['password'],
            'authKey' => '',
            'accessToken' =>'',];
        return new static($identity_use);
    }
    return null;
}
像这样试试

  • 实现
    implements\yii\web\IdentityInterface
    为活动 记录并实施
    \yii\web\IdentityInterface
    中的所有方法
  • 将$users声明为静态变量,即
    private static$users=[]在你的课堂上
  • 为获取用户详细信息表单用户名更改
    findByUsername()
    函数。 在配置文件中添加

    'user'=>['identityClass'=>'app\models\active record class', “enableAutoLogin”=>true, ],

    }


  • 我也有同样的问题。我的自定义类实现了yii\web\IdentityInterface。我确保会话已启用,enableAutologin也为真,但我一点运气都没有

    最后,我意识到在config/web.php中有一个设置'user'=>


    当然,identityClass是基本yii应用程序中的默认值。将此值设置为我的自定义类后,标识最终被保留。

    同样的问题,在3天后找到解决方案,我发现它工作正常。 我的用户模型是:

    public static function findIdentity($id) {
    
    }
    
    我改变:

    public static function findIdentity($id) {
        return self::findOne(array('id'=>$id));
    }
    
    致:


    另一个可能导致会话丢失的简单错误是呼叫

    return $this->redirect(...);
    
    而不是


    由于Yii在此方法中不调用
    die()
    exit()
    ,因此重定向后的行上的代码将被执行。由于习惯了CodeIgniter,我假设了这一点,花了好几天时间才弄明白。

    忘了提到我没有看到Yii::$app->session中存储的身份信息。因此,不确定在请求之间通常如何维护标识。我在使用标准的基于activerecord的标识时跟踪了所有相同的代码,该标识即使在登录重定向后也能正常工作。但我还是看不出有什么不同:在SiteController::actionLogin()返回之前,标识一直存在,在本例中,之后也存在。activerecord是否可以明确地在请求之间保留标识(我必须在代码中复制该标识)?您是使用Yii默认标识类还是实现了自己的标识类?如果您实现了自己的post,请为其编写代码。否则,为您的模型发布代码。可能您的会话存储有问题。可能无法保存您的会话。感谢您的回复!nterms:如果它是会话存储,那么只有当我不使用activerecord类时,它才是一个会话问题,因为当我使用AR类时,一切都很好。米哈伊普:我确实创建了自己的标识类,我很快就会发布代码。我不知道yii提供了一个默认的实现类IdentityInterface,它不使用activerecord。如果是这样,我将很高兴地将其子类化。我在模型中使用活动记录:类User extends\yii\db\ActiveRecord implements\yii\web\IdentityInterface。我认为模型在重定向后会丢失会话参数而不保存。通过这种方式,我在会话Yii中推送了一些变量:$app->session['user.'$this->id.'notification']=$n。在我的应用程序中,我应该在显示后清除通知。我清除它getter-Yii::$app->session['user'.$this->id.\u notification']=NULL;更改findIdentity函数以实际返回用户实例也解决了我的问题谢谢!在我找到这个之前我一直在兜圈子。
    public static function findIdentity($id) {
    
    }
    
    public static function findIdentity($id) {
        return self::findOne(array('id'=>$id));
    }
    
    $this->redirect(...);
    
    return $this->redirect(...);