Yii2框架实现登录、退出及自动登录成果的要领详解
副问题[/!--empirenews.page--]
本篇章节讲授Yii2框架实现登录、退出及自动登录成果的要领。分享给各人供各人参考,详细如下: 自动登录的道理很简朴。首要就是操作cookie来实现的 在第一次登录的时辰,假如登录乐成而且选中了下次自动登录,那么就会把用户的认证信息生涯到cookie中,cookie的有用期为1年可能几个月。 在下次登录的时辰先判定cookie中是否存储了用户的信息,假若有则用cookie中存储的用户信息来登录, 设置User组件 起首在设置文件的components中配置user组件 [ 'identityClass' => 'appmodelsUser','enableAutoLogin' => true,],我们看到enableAutoLogin就是用来判定是否要启用自动登录成果,这个和界面上的下次自动登录无关。 只有在enableAutoLogin为true的环境下,假如选择了下次自动登录,那么就会把用户信息存储起来放到cookie中并配置cookie的有用期为3600*24*30秒,以用于下次登录 此刻我们来看看Yii中是奈何实现的。 一、第一次登录存cookie 1、login 登录成果beforeLogin($identity,false,$duration)) { $this->switchIdentity($identity,$duration); $id = $identity->getId(); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip with duration $duration.",__METHOD__); $this->afterLogin($identity,$duration); } return !$this->getIsGuest(); }在这里,就是简朴的登录,然后执行switchIdentity要领,配置认证信息。 2、switchIdentity配置认证信息getSession(); if (!YII_ENV_TEST) { $session->regenerateID(true); } $this->setIdentity($identity); $session->remove($this->idParam); $session->remove($this->authTimeoutParam); if ($identity instanceof IdentityInterface) { $session->set($this->idParam,$identity->getId()); if ($this->authTimeout !== null) { $session->set($this->authTimeoutParam,time() + $this->authTimeout); } if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity,$duration); } } elseif ($this->enableAutoLogin) { Yii::$app->getResponse()->getCookies()->remove(new Cookie($this->identityCookie)); } }这个要领较量重要,在退出的时辰也必要挪用这个要领。 这个要领首要有三个成果 ① 配置session的有用期 ② 假如cookie的有用期大于0而且应承自动登录,那么就把用户的认证信息生涯到cookie中 ③ 假如应承自动登录,删除cookie信息。这个是用于退出的时辰挪用的。退出的时辰转达进来的$identity为null identityCookie); $cookie->value = json_encode([ $identity->getId(),$identity->getAuthKey(),$duration,]); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }存储在cookie中的用户信息包括有三个值:
getId()和getAuthKey()是在IdentityInterface接口中的。我们也知道在配置User组件的时辰,这个User Model是必必要实现IdentityInterface接口的。以是,可以在User Model中获得前两个值,第三值就是cookie的有用期。 二、自动从cookie登录 从上面我们知道用户的认证信息已经存储到cookie中了,那么下次的时辰直接从cookie内里守信息然后配置就可以了。 1、AccessControl用户会见节制Yii提供了AccessControl来判定用户是否登录,有了这个就不必要在每一个action内里再判定了 [ 'class' => AccessControl::className(),'only' => ['logout'],'rules' => [ [ 'actions' => ['logout'],'allow' => true,'roles' => ['@'],]; }2、getIsGuest、getIdentity判定是否定证用户isGuest是自动登录进程中最重要的属性。 在上面的AccessControl会见节制内里通过IsGuest属性来判定是否是认证用户,然后在getIsGuest要领内里是挪用getIdentity来获取用户信息,假如不为空就声名是认证用户,不然就是旅客(未登录)。 getIdentity($checkSession) === null; } public function getIdentity($checkSession = true) { if ($this->_identity === false) { if ($checkSession) { $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }3、renewAuthStatus 从头天生用户认证信息getSession(); $id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->idParam) : null; if ($id === null) { $identity = null; } else { /** @var IdentityInterface $class */ $class = $this->identityClass; $identity = $class::findIdentity($id); } $this->setIdentity($identity); if ($this->authTimeout !== null && $identity !== null) { $expire = $session->get($this->authTimeoutParam); if ($expire !== null && $expire < time()) { $this->logout(false); } else { $session->set($this->authTimeoutParam,time() + $this->authTimeout); } } if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }这一部门先通过session来判定用户,由于用户登录后就已经存在于session中了。然后再判定假如是自动登录,那么就通过cookie信息来登录。 4、通过生涯的Cookie信息来登录 loginByCookieidentityCookie['name']; $value = Yii::$app->getRequest()->getCookies()->getValue($name); if ($value !== null) { $data = json_decode($value,true); if (count($data) === 3 && isset($data[0],$data[1],$data[2])) { list ($id,$authKey,$duration) = $data; /** @var IdentityInterface $class */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity !== null && $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,$duration); } } elseif ($identity !== null) { Yii::warning("Invalid auth key attempted for user '$id': $authKey",__METHOD__); } } } }先读取cookie值,然后 这个从上面的代码可以知道要想实现自动登录,这三个值都必需有值。其它,在User Model中还必必要实现findIdentity、validateAuthKey这两个要领。 登录完成后,还可以再从头配置cookie的有用期,这样便能一路有用下去了。 switchIdentity($identity,$this->autoRenewCookie ? $duration : 0);(编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |