Php Yii2用户身份在登录后变为null
我有一个Yii2应用程序,在其中我从第三方API验证用户,一旦用户通过身份验证,我就登录用户并设置身份,一切正常。但当我导航到任何其他页面时,用户标识将变为null,用户将成为来宾用户 这是我的登录表:Php Yii2用户身份在登录后变为null,php,yii,yii2,Php,Yii,Yii2,我有一个Yii2应用程序,在其中我从第三方API验证用户,一旦用户通过身份验证,我就登录用户并设置身份,一切正常。但当我导航到任何其他页面时,用户标识将变为null,用户将成为来宾用户 这是我的登录表: <?php namespace app\models; use app\components\RestBehavior; use app\models\web\LoginIdentity; use Yii; use yii\base\Model; use yii\helpers\Jso
<?php
namespace app\models;
use app\components\RestBehavior;
use app\models\web\LoginIdentity;
use Yii;
use yii\base\Model;
use yii\helpers\Json;
use yii\helpers\VarDumper;
use yii\web\IdentityInterface;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $email;
public $password;
public $company_id;
public $companies=[];
public $token;
private $_user = false;
public function behaviors()
{
return [
RestBehavior::class
];
}
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['email', 'password','company_id'], 'required'],
];
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
return Yii::$app->user->login($this->getUser(), 0);
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = LoginIdentity::findUser($this->attributes);
}
return $this->_user;
}
}
这与Yii2或标识类实现无关。代码在任何意义上都是正确的。原因是,我的服务器已将cookie\u secure
设置为true,这意味着只能在SSL上建立会话,但我尝试在不安全的来源(即http)上登录。
此外,我的站点正在使用SSL,但在加载页面时,由于favicon路径无效,它变成了不安全或http。
因此,firefox浏览器在控制台中显示,当客户端在HTTP上运行时,服务器会拒绝cookie,而Chrome的控制台则对幕后发生的事情保持沉默。但当我导航到任何其他页面时,用户身份将变为空,用户将成为来宾用户。。。非常奇怪的是,整个代码在我的本地计算机(即本地主机)上运行良好。但在服务器上,用户get登录,但在访问任何新的经过身份验证的路由时注销。所以你只在服务器上有这个问题?如果在本地pc上工作正常,那么它必须是服务器吗?这就是为什么您必须实现\yii\web\IdentityInterface
指定的功能的原因。它不起作用的原因可能是您没有实现这些功能。我假设Yii调用findByUsername
来设置LoginIdentity。但是您没有返回任何内容,因此它被解释为null。
<?php
namespace app\models\web;
use app\components\RestBehavior;
use \Yii;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\helpers\VarDumper;
class LoginIdentity extends \yii\base\BaseObject implements \yii\web\IdentityInterface
{
public $resource_id;
public $token;
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
RestBehavior::class
];
}
public static function findUser($params)
{
if(!empty(Yii::$app->user->identity))
{
return new static([
'resource_id'=>self::getId(),
'token'=>self::getAuthKey()
]);
}
else
{
Yii::$app->controller->sendPOST('/login',$params);
if(Yii::$app->response->statusCode===200)
{
$response=Json::decode(Yii::$app->response->data);
$identity=new static([
'resource_id'=>$response['resource']['resource_id'],
'token'=>$response['token']
]);
Yii::$app->session->set('resource_id',$response['resource']['resource_id']);
Yii::$app->session->set('token',$response['token']);
return $identity;
}
return null;
}
}
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
{
$session=Yii::$app->session;
if(!empty($session['resource_id'])&&!empty($session['token']))
{
return new static([
'resource_id'=>$session['resource_id'],
'token'=>$session['token']
]);
}
return null;
}
/**
* {@inheritdoc}
* @param \Lcobucci\JWT\Token $token
*/
public static function findIdentityByAccessToken($token, $type = null)
{
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->resource_id;
}
/**
* {@inheritdoc}
*/
public function getAuthKey()
{
return $this->token;
}
/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
}
}