Pure js achieve inter-system auto-login feature

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44781409/article/details/102776747

A. Prerequisite for the use of this method

  1. Among domain name system must be the same.

    For example http: // vue.deppontest.com:9000/#/ automatically log on to http: // angular.deppontest.com:8080/#/

  2. Cookie with back-end system is required to log check Log

II. If the above conditions are met can be used to copy this code

     // 查看是否登录过有cookie
      const tokeen = getCookie("Admin-Token");
      if (tokeen !== null) {
      // 如果有可直接自动登录
        window.open(
         `http://XXX.deppontest.com:XXXX/#/corelinappname=${
         this.param.appName
          }`,"_blank"
        );
      } else {
          // 若没有先获取cookie写入后自动登录
          // 需要登录系统的用户名 和 密码        
        let parmars = { username: " ", password: " " }; 
        const url = "http:// XXX .deppontest.com:XXXX/login/";
        this.$http.post(url, parmars).then(res => {
          let token = "若登录系统token固定可固定写死";
          if (res.data && res.data.token) {
            token = res.data.token;
          }
          // 修改cookie 填充token 可以访问以.deppontest.com 域名的网站
          document.cookie = `Admin-Token=${token};path=/;domain=.deppontest.com;expires=`;
          //链接到需要登录的系统
          window.open(
            `http://XXX.deppontest.com:XXXX/#/corelink?appname=${
              this.param.appName
            }`,"_blank"
          );
        });
      }

Done, effective pro-test

Guess you like

Origin blog.csdn.net/weixin_44781409/article/details/102776747