皎然
- 皎然 2017-09-28 已签到连续签到7天,获得了20个金钱
- 皎然 赞了话题[Yii2笔记]001说声Hello
- 皎然 发表了说说恩
- 皎然 赞了回答
先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
- 皎然 发表了说说签到有什么好礼
- 皎然 2017-09-27 已签到连续签到6天,获得了20个金钱
- 皎然 发表了说说session解决不了
- 皎然 2017-09-26 已签到连续签到5天,获得了20个金钱
- 皎然 发表了说说用谷歌智能代理
- 皎然 2017-09-25 已签到连续签到4天,获得了20个金钱