对YII2的登录验证是在不是很清楚,我总结了几个问题,希望大神帮忙回答一下 [ 2.0 版本 ]
登录方式是不是一共有三种?一种是账号密码,一种是cookie,一种是token。
账号密码登录我能理解,但是cookie登录,YII2具体是怎么实现的呢?
我大概步骤是能了解,就是把弄个认证类,然后认证类里就写getAuthKey()
和validateAuthKey($authKey)
,然后就能实现自动登录?那cookie的时间怎么设置的呀?。。。。。。我很懵逼呀,求大神帮忙,给我一个连接,我自己去看也成,我实在找不到了。最好是连认证,授权一起给我讲一下,或者给我丢个连接,我自己看,文档我看了N遍了,过程我能理解,就是不知道具体实现。
最佳答案
-
先说自动登录,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功能,跟踪代码绝对杠杠的
共 7 条回复皎然 觉得很赞
其他 2 个回答
-
完整的具体实现在高级版脚手架那块。
按照你所理解的登录,是两种。token是验证 API 授权才需要用到的。
你可以参考这个连接http://www.yiichina.com/tutorial/965,它拥有无比完整的代码。然后把登录流程看一遍(在vendor目录下的代码可以不用看),再结合权威指南的安全-》认证那块。你上述的所有问题必定都可以在这两块得到解决。共 6 条回复 -
数字派 北京
最后登录:2023-03-07
在线时长:52小时34分
- 粉丝10
- 金钱1515
- 威望10
- 积分2135