Data-id= "1190000004999380" data-licence= "" >
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. Login method modification to login module sitecontroller.php
1.1. Pre-modification code
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. 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' => ['domain' => '.' . DOMAIN, 'lifetime' => 0], 'timeout' => 3600, ],
2.2. Modified Code
'session' => [ 'cookieParams' => ['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.
Related information
Yii2 Configuring a cross-domain login instance: http://www.kuitao8.com/20150507/3735.shtml
Yii2 How to use Redirect to let the page automatically jump to the outside station? : https://segmentfault.com/q/1010000002549004
Complete the full text, if there is insufficient or better ways and means, welcome everyone enthusiastically put forward, we exchange learning with each other.
The above describes the further optimization of YII2 cross-domain SSO landing, including the sso,yii aspects of the content, I hope that the PHP tutorial interested in a friend helpful.