Home > Backend Development > PHP Tutorial > 进一步优化Yii2跨域的SSO登陆

进一步优化Yii2跨域的SSO登陆

WBOY
Release: 2016-06-23 13:08:36
Original
907 people have browsed it

序言

本文主要是对“关于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,                ]);            }        }
Copy after login

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,                ]);            }        }    }
Copy after login

2、修改session的过期时间,设置到极大值。

commonconfigmain.php

2.1、修改前代码

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

2.2、修改后代码

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

常见问题

1、虽然session的过期时间已经设置了极大值,登陆成功cookie也有值了,但是登陆后大约过两个小时左右还是会退出登陆,为何会这样? Yii2如何实现真正的永久登陆,还请大牛们解答,后续要是我解决了也会继续更新,一同进步。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template