本文實(shí)例講述了Yii2框架實(shí)現(xiàn)登錄、退出及自動(dòng)登錄功能的方法。分享給大家供大家參考,具體如下:
自動(dòng)登錄的原理很簡單。主要就是利用cookie來實(shí)現(xiàn)的
在第一次登錄的時(shí)候,如果登錄成功并且選中了下次自動(dòng)登錄,那么就會(huì)把用戶的認(rèn)證信息保存到cookie中,cookie的有效期為1年或者幾個(gè)月。
在下次登錄的時(shí)候先判斷cookie中是否存儲(chǔ)了用戶的信息,如果有則用cookie中存儲(chǔ)的用戶信息來登錄,
配置User組件
首先在配置文件的components中設(shè)置user組件
'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ],
我們看到enableAutoLogin就是用來判斷是否要啟用自動(dòng)登錄功能,這個(gè)和界面上的下次自動(dòng)登錄無關(guān)。
只有在enableAutoLogin為true的情況下,如果選擇了下次自動(dòng)登錄,那么就會(huì)把用戶信息存儲(chǔ)起來放到cookie中并設(shè)置cookie的有效期為3600*24*30秒,以用于下次登錄
現(xiàn)在我們來看看Yii中是怎樣實(shí)現(xiàn)的。
一、第一次登錄存cookie
1、login 登錄功能
public function login($identity, $duration = 0) { if ($this->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, false, $duration); } return !$this->getIsGuest(); }
在這里,就是簡單的登錄,然后執(zhí)行switchIdentity方法,設(shè)置認(rèn)證信息。
2、switchIdentity設(shè)置認(rèn)證信息
public function switchIdentity($identity, $duration = 0) { $session = Yii::$app->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)); } }
這個(gè)方法比較重要,在退出的時(shí)候也需要調(diào)用這個(gè)方法。
這個(gè)方法主要有三個(gè)功能
① 設(shè)置session的有效期
② 如果cookie的有效期大于0并且允許自動(dòng)登錄,那么就把用戶的認(rèn)證信息保存到cookie中
③ 如果允許自動(dòng)登錄,刪除cookie信息。這個(gè)是用于退出的時(shí)候調(diào)用的。退出的時(shí)候傳遞進(jìn)來的$identity為null
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ]); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
存儲(chǔ)在cookie中的用戶信息包含有三個(gè)值:
$identity->getId()
$identity->getAuthKey()
$duration
getId()和getAuthKey()是在IdentityInterface接口中的。我們也知道在設(shè)置User組件的時(shí)候,這個(gè)User Model是必須要實(shí)現(xiàn)IdentityInterface接口的。所以,可以在User Model中得到前兩個(gè)值,第三值就是cookie的有效期。
二、自動(dòng)從cookie登錄
從上面我們知道用戶的認(rèn)證信息已經(jīng)存儲(chǔ)到cookie中了,那么下次的時(shí)候直接從cookie里面取信息然后設(shè)置就可以了。
1、AccessControl用戶訪問控制
Yii提供了AccessControl來判斷用戶是否登錄,有了這個(gè)就不需要在每一個(gè)action里面再判斷了
public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout'], 'rules' => [ [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; }
2、getIsGuest、getIdentity判斷是否認(rèn)證用戶
isGuest是自動(dòng)登錄過程中最重要的屬性。