Further optimize YII2 cross-domain SSO login

Source: Internet
Author: User

Preface

This article is mainly on the "about YII2 how to realize the cross-domain SSO landing Resolution" improvement, because in that article I have written the SSO landing basic implementation process, is now further optimization. The main optimization of the part has two points: first, in the www.XXX.com landing status of the page address bar input login.XXX.com back to the landing page, but do not exit the login, and then input www.XXX.com time, let it back to the www.XXX.com page and maintain the status of the landing; Modifies the session expiration time, set to the maximum value.

Demand analysis

1. Enter Login. XXX.com just lets it go back to the landing interface, but does not exit the login, the session and the value of the cookie is still preserved. 2, to achieve permanent landing status. As long as you do not click to exit the landing, has been kept in the state.

Code Analysis

1, to login module sitecontroller.php Login Method Modification 1.1, the pre-modification code

Public Function Actionlogin ()        {               $URL =yii:: $app->request->get (' RedirectURL ');              $model = new LoginForm ();            if (!\yii:: $app->user->isguest) {                 $this->actionlogout ();//Exit login                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. Modified Code

 Public Function Actionlogin () {$URL =yii:: $app->request->get (' RedirectURL '); $URL 1= ' http://'.        DOMAIN_CRM; $URL 2= ' http://'.        DOMAIN_HR; $URL 3= ' http://'.        Domain_admin; $URL 4= ' http://'.               Domain_oa; $redirectURL 1= ' http://'. Domain_login. '? redirecturl=http://'.        DOMAIN_CRM; $redirectURL 2= ' http://'. Domain_login. '? redirecturl=http://'.        DOMAIN_HR; $redirectURL 3= ' http://'. Domain_login. '? redirecturl=http://'.        Domain_admin; $redirectURL 4= ' http://'. Domain_login. '? redirecturl=http://'.        Domain_oa;        $model = new LoginForm (); Verify that you are logged in, non-empty is logged on if (!\yii:: $app->user->isguest) {if (!empty ($URL)) {$this->actio                Nlogout ();//Exit Login if ($URL = = $URL 2) {return $this->redirect ($redirectURL 2);                }elseif ($URL = = $URL 3) {return $this->redirect ($redirectURL 3); } if ($URL = = $URL 4) {REturn $this->redirect ($redirectURL 4);           } return $this->redirect ($redirectURL 1); }else{//redirecturl does not exist, the submission form determines if (Domain_login) {if ($model->load (Y                             II:: $app->request->post ()) && $model->login ()) {if (empty ($URL)) {                                     return $this->redirect ($URL 1,301);                            }else{if ($URL = = $URL 2) {return $this->redirect ($URL 2,301);                            }elseif ($URL = = $URL 3) {return $this->redirect ($URL 3,301); } if ($URL = = $URL 4) {return $this->redir                            ECT ($URL 4,301);                        } return $this->redirect ($URL 1,301);        }} else {                 return $this->renderpartial (' login ', [' model ' = $model,                    ]);                }}else{return $this->gohome ();//The main difference from the previous code is here, landing let it go back to the landing page. }}} or else {//redirecturl exists, submit the form to determine if ($model->load (Yii:: $app->request ->post ()) && $model->login ()) {if (empty ($URL)) {return $this->redirec                             T ($URL 1,301);                    }else{if ($URL = = $URL 2) {return $this->redirect ($URL 2,301);                    }elseif ($URL = = $URL 3) {return $this->redirect ($URL 3,301);                    } if ($URL = = $URL 4) {return $this->redirect ($URL 4,301);                } return $this->redirect ($URL 1,301); }} ELSE {return $this->renderpartial (' login ', [' model ' = $model,])            ; }        }    }

2, modify the session expiration time, set to the maximum value.

commonconfigmain.php

2.1. Pre-modification code

' Session ' = [                       ' cookieparams ' and ' = ' domain ' = '. ' DOMAIN, ' lifetime ' = 0],                        ' timeout ' = 3600,        ],

2.2. Modified Code

' Session ' = [                       ' cookieparams ' and ' = ' domain ' = '. ' DOMAIN, ' lifetime ' = 0],                        ' timeout ' = 7200,        ],

Problems

1, although the expiration time of the session has been set a maximum value, the login success cookie also has value, but after landing about two hours or so will exit the landing, why this? Yii2 How to achieve real permanent landing, but also ask Daniel to answer , follow-up if I solve will continue to update, progress together.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.