進一步最佳化Yii2跨域的SSO登陸

來源:互聯網
上載者:User

序言

本文主要是對“關於Yii2如何?跨域的SSO登陸的解析”的改進,因為在那篇文章中我已經寫出了SSO登陸的基本實現過程,現在是進一步最佳化。主要最佳化的部分有兩點:一、在www.XXX.com登陸狀態的頁面地址欄上輸入login.XXX.com的時候回到登陸頁面,但並不退出登陸,再輸入www.XXX.com的時候,讓它又回到了www.XXX.com頁面並保持登陸的狀態;二、修改session的到期時間,設定到極大值。

需求分析

1、輸入login.XXX.com只是讓它回到登陸的介面,但並不會退出登陸,session和cookie的值還是儲存著的。2、實現永久登陸狀態。只要不點擊退出登陸,就一直保持著登陸狀態。

程式碼分析

1、對login模組SiteController.php的Login方法修改1.1、修改前代碼

public function actionLogin()        {               $URL=Yii::$app->request->get('redirectURL');              $model = new LoginForm();            if (!\Yii::$app->user->isGuest) {                 $this->actionLogout();//退出登陸                return $this->redirect('http://'.DOMAIN_LOGIN.'?redirectURL=http://'.DOMAIN_HOME);            }                     if ($model->load(Yii::$app->request->post()) && $model->login()) {                if(empty($URL)){                    return $this->redirect('http://'.DOMAIN_HOME,301);                }else{                    return $this->redirect($URL,301);                }                           // return $this->goBack();            } else {                 return $this->renderPartial('login', [                    'model' => $model,                ]);            }        }

1.2、修改後代碼

 public function actionLogin()    {          $URL=Yii::$app->request->get('redirectURL');          $URL1='http://'.DOMAIN_CRM;        $URL2='http://'.DOMAIN_HR;        $URL3='http://'.DOMAIN_ADMIN;        $URL4='http://'.DOMAIN_OA;               $redirectURL1='http://'.DOMAIN_LOGIN.'?redirectURL=http://'.DOMAIN_CRM;        $redirectURL2='http://'.DOMAIN_LOGIN.'?redirectURL=http://'.DOMAIN_HR;        $redirectURL3='http://'.DOMAIN_LOGIN.'?redirectURL=http://'.DOMAIN_ADMIN;        $redirectURL4='http://'.DOMAIN_LOGIN.'?redirectURL=http://'.DOMAIN_OA;        $model = new LoginForm();        //驗證是否已登入,非空為登入        if (!\Yii::$app->user->isGuest) {            if(!empty($URL)){                $this->actionLogout();//退出登陸                if($URL==$URL2){                    return $this->redirect($redirectURL2);                }elseif($URL==$URL3){                    return $this->redirect($redirectURL3);                }                if($URL==$URL4){                    return $this->redirect($redirectURL4);                }                return $this->redirect($redirectURL1);           }else{                //redirectURL不存在時,提交表單判斷                if(DOMAIN_LOGIN){                      if ($model->load(Yii::$app->request->post()) && $model->login()) {                        if(empty($URL)){                             return $this->redirect($URL1,301);                                     }else{                            if($URL==$URL2){                                return $this->redirect($URL2,301);                            }elseif($URL==$URL3){                                return $this->redirect($URL3,301);                            }                            if($URL==$URL4){                                return $this->redirect($URL4,301);                            }                            return $this->redirect($URL1,301);                        }                               } else {                         return $this->renderPartial('login', [                            'model' => $model,                        ]);                    }                }else{                     return $this->goHome(); //與之前的代碼主要的區別在這裡,登陸就讓它回到登陸頁面。                }             }          } else {             //redirectURL存在時,提交表單判斷            if ($model->load(Yii::$app->request->post()) && $model->login()) {                if(empty($URL)){                     return $this->redirect($URL1,301);                             }else{                    if($URL==$URL2){                        return $this->redirect($URL2,301);                    }elseif($URL==$URL3){                        return $this->redirect($URL3,301);                    }                    if($URL==$URL4){                        return $this->redirect($URL4,301);                    }                    return $this->redirect($URL1,301);                }                       } else {                 return $this->renderPartial('login', [                    'model' => $model,                ]);            }        }    }

2、修改session的到期時間,設定到極大值。

commonconfigmain.php

2.1、修改前代碼

'session' => [                       'cookieParams' => ['domain' => '.' . DOMAIN, 'lifetime' => 0],                        'timeout' => 3600,        ],

2.2、修改後代碼

'session' => [                       'cookieParams' => ['domain' => '.' . DOMAIN, 'lifetime' => 0],                        'timeout' => 7200,        ],

常見問題

1、雖然session的到期時間已經設定了極大值,登陸成功cookie也有值了,但是登陸後大約過兩個小時左右還是會退出登陸,為何會這樣? Yii2如何?真正的永久登陸,還請大牛們解答,後續要是我解決了也會繼續更新,一同進步。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.