实现步骤 1、在commonconfigmain.php文件配置如下:
[ 'user' => [ 'identityclass' => 'login\models\user', 'enableautologin' => true, 'identitycookie' => ['name' => '_identity', 'httponly' => true,'domain' => '.' . domain], // 'returnurl'=>'//' . domain_home, ], 'session' => [ 'cookieparams' => ['domain' => '.' . domain, 'lifetime' => 0], 'timeout' => 24*3600*30, ],
2、新建一个login模块,然后打开commonconfigbootstrap.php加下这么一段代码:
yii::setalias('login', dirname(dirname(__dir__)) . '/login'); //增加自定义目录结构
3、在loginconfigmain.php里修改 urlmanager,改成下面这样子:
'urlmanager' => [ 'class' => 'common\components\mutilpledomainurlmanager', 'domains' => [ 'crm' => '//' . domain_crm, 'admin' => '//' . domain_admin, 'hr' => '//' . domain_hr, 'oa' => '//' . domain_oa, 'frontend' => '//' . domain_frontend, 'backend' => '//' . domain_backend, // 'img' => '//' . domain_img, 'api' => '//' . domain_api, 'login' => '//' . domain_login, ], //'baseurl' => 'http://'.domain_login.'?redirecturl=http://'.domain_home, 'showscriptname' => false, 'enableprettyurl' => true, //美化url 'enablestrictparsing' => true, //设置有无‘s’; // 'suffix' => .php, 'rules' => [ '' => 'site/login', // 如果没有这里,则访问域名不能直接打开默认action (去除url的“site/login”) ] ],
4、补充第3步骤缺少的mutilpledomainurlmanager.php文件mutilpledomainurlmanager.php,这个文件按照我给你们的命名空间存放。
getbaseurl(); if ($domain) { if (!isset($this->domains[$domain])) { throw new \yii\base\invalidconfigexception('please configure urlmanager of domain ' . $domain . '.'); } $this->setbaseurl($this->domains[$domain]); } $url = parent::createurl($params); $this->setbaseurl($bak); return $url; }}
注释:用于获取domain url。5、修改login模块下的sitecontroller.php login方法
//登录 public function actionlogin() { //获取当前的url $url=yii::$app->request->gethostinfo().yii::$app->request->url; $url1='http://'.domain_crm; $redirecturl=yii::$app->request->get('redirecturl'); $redirecturl1='http://'.domain_login; $model = new loginform(); tagdependency::invalidate(yii::$app->cache, ['session:'.yii::$app->session->id]); //验证是否已登录,非空为登录 if (!\yii::$app->user->isguest) { if(!empty($redirecturl)){ $this->actionlogout();//强制性退出登录 return $this->redirect($url); }else{ //redirecturl不存在时,提交表单判断 if($this->sitelogin){ if ($model->load(yii::$app->request->post()) && $model->login()) { //判断该账号是否禁止登录 if(empty($t_status=$model->user->attributes['t_status']) && $t_status==0){ return $this->error($redirecturl1,[yii::t('yii','the account is prohibited from logging in, please contact the administrator!')]); }else{ if(empty($redirecturl)) return $this->redirect($url1,301); return $this->redirect($redirecturl,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($t_status=$model->user->attributes['t_status']) && $t_status==0){ if(empty($redirecturl)){ return $this->error($redirecturl1,[yii::t('yii','the account is prohibited from logging in, please contact the administrator!')]); } return $this->error($url,[yii::t('yii','the account is prohibited from logging in, please contact the administrator!')]); }else{ if(empty($redirecturl)) return $this->redirect($url1,301); return $this->redirect($redirecturl,301); } } else { return $this->renderpartial('login', [ 'model' => $model, ]); } } }
6、修改frontend模块下的sitecontroller.php login方法
public function actionlogin() { //获取上一个url $url=yii::$app->request->gethostinfo().yii::$app->user->getreturnurl(); if (!\yii::$app->user->isguest) { return $this->redirect('http://'.domain_login.'?redirecturl='.$url); } $model = new loginform(); if ($model->load(yii::$app->request->post()) && $model->login()) { return $this->goback(); } else { if(!empty($url)){ return $this->redirect('http://'.domain_login.'?redirecturl='.$url); }else{ return $this->renderpartial('login', [ 'model' => $model, ]); } } }
7、在frontendviewsdefaultlayoutsmain.php的顶部加入下面代码
$redirecturl='http://'.domain_login.'?redirecturl='.yii::$app->request->gethostinfo().yii::$app->request->url;
8、最后在退出的a标签这么输出。
注:在其它模块如:backend、crm等等当中仿造我这frontend的实现思路改改,即可实现整个项目的sso登录机制。
提醒注意
1、在第1步骤中,动态获取无www的域名,此步骤必须做域名的判断处理,比如:www.xxx.com,www.xxx.com.cn,www.xxx.com:8099等这些可能出现的域名,以保证域名都能使用实现登录退出的机制。
2、在第5步骤和第7步骤中,使用yii2自带的方法yii::$app->request->gethostinfo().yii::$app->request->url获取当前的url,是比较方便且高效的一种做法,能降低代码的冗余。
3、在第6步骤中的frontend模块下的sitecontroller.php login方法里,用yii2自带的方法yii::$app->request->gethostinfo().yii::$app->user->getreturnurl()获取上一个url,这里必须特别注意是获取“上一个url”而不是当前的url,获取当前的url就变成了login.xxx.com了,这是不对的。