Brief description of SSO login logic for Yii2 cross-origin
This article describes how to implement SSO login in yii2. In fact, before that, I have written two articles about SSO login implementation methods and further optimization. This article also introduces SSO login of Yii2, which is constantly summarized and shared, the purpose is to maximize the SSO login function of Yii2, consider the problem from the perspective of program development flexibility, and eliminate all potential limitations in the cradle.
Steps
1. configure the following in the commonconfigmain. php file:
['User' => ['identityclass' => 'Login \ models \ user', 'enableautologin' => true, 'identitycookies' => ['name' => '_ identity', 'httpponly '=> true, 'domain' => '. '. DOMAIN], // 'returnurl' => '//'. DOMAIN_HOME,], 'session '=> ['cookieparams' => ['domain '=> '. '. DOMAIN, 'lifetime' => 0], 'timeout' => 24*3600*30,],
2. create a login module and open commonconfigbootstrap. php to add the following code:
Yii: setAlias ('login', dirname (_ DIR _). '/login'); // add a custom directory structure.
3. modify urlManager in loginconfigmain. php as follows:
'Urlmanager' => ['class' => 'common \ components \ mutilpledomainurlmanager', 'domains' => ['CRM '=> '//'. DOMAIN_CRM, 'admin' => '//'. DOMAIN_ADMIN, 'HR' => '//'. DOMAIN_HR, 'a' => '//'. 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, // beautify the URL 'enablestrictparsing' => true, // Set whether or not 'ss' is available '; // 'suffix '=> ". php ", 'rules' => ['' => 'site/login', // If not, the default Action cannot be opened directly for the access domain name (remove the URL's "site/login")],
4. add the MutilpleDomainUrlManager. php file that is missing in step 3rd. the file will be stored in your namespace as per your namespace.
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; }}
Note: used to obtain the domain url. 5. modify SiteController. php login method under the Login module
// Log on to the public function actionLogin () {// Obtain the current 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]); // verify if you have logged on. if (! \ Yii: $ app-> user-> isGuest) {if (! Empty ($ redirectURL) {$ this-> actionLogout (); // forcibly exit logon return $ this-> redirect ($ URL);} when else {// redirectURL does not exist, submit a form to determine if ($ this-> siteLogin) {if ($ model-> load (Yii: $ app-> request-> post ()) & $ model-> login ()) {// Determine whether the account is prohibited from logging on to 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,]) ;}} when else {return $ this-> goHome () ;}} else {// redirectURL exists, submit a form to judge if ($ model-> load (Yii :: $ app-> request-> post () & $ model-> login ()) {// Determine whether the account is prohibited from logging on to 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. modify the SiteController. php Login method under the frontend module.
Public function actionLogin () {// Get the previous 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. add the following code to the top of frontendviewsdefaultlayoutsmain. php.
$redirectURL='http://'.DOMAIN_LOGIN.'?redirectURL='.Yii::$app->request->getHostInfo().Yii::$app->request->url;
8. Finally, the output of the exit a tag is as follows: .
Note: in other modules, such as backend and crm, the frontend implementation idea can be modified to implement the SSO login mechanism for the entire project.
Reminder
1. in step 1st, dynamic acquisition of domain names without www, this step must be done to determine the domain name, such as: www.xxx.com, www.xxx.com.cn, www.xxx.com: 8099 and other possible domain names, to ensure that all domain names can use the login and exit mechanism.
2. in steps 5th and 7th, use the Yii2 method Yii: $ app-> request-> getHostInfo (). yii: $ app-> request-> url to obtain the current url is a convenient and efficient method that can reduce code redundancy.
3. SiteController under the frontend module in step 6th. in the php Login method, use the Yii2 built-in method Yii: $ app-> request-> getHostInfo (). yii: $ app-> user-> getReturnUrl () to get the previous url. Note that the previous url is obtained instead of the current url, obtaining the current url becomes login.xxx.com, which is incorrect.