fastadmin会员单点登录

发布于:2025-07-17 ⋅ 阅读:(20) ⋅ 点赞:(0)

使用fastadmin会员单点登录

按照手册上,只能实现后端的单点登录,修改配置文件,但是在对接一些前后端分离的项目时,需要实现接口的单点登录,按照原有配置,不能满足,其实只用修改一个系统文件就可以。

1.找到关键文件

app\common\library\Auth.php

2.在指定位置添加清除id的token

大概是在234行

//清除之前的token,实现单点登录
Token::clear($user->id);

具体代码位置

 /**
 * 用户登录
   *
   * @param string $account  账号,用户名、邮箱、手机号
   * @param string $password 密码
   * @return boolean
   */
  public function login($account, $password)
  {
      $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
      $user = User::get([$field => $account]);
      if (!$user) {
          $this->setError('Account is incorrect');
          return false;
      }

      if ($user->status != 'normal') {
          $this->setError('Account is locked');
          return false;
      }

      if ($user->loginfailure >= 10 && time() - $user->loginfailuretime < 86400) {
          $this->setError('Please try again after 1 day');
          return false;
      }

      if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
          $user->save(['loginfailure' => $user->loginfailure + 1, 'loginfailuretime' => time()]);
          $this->setError('Password is incorrect');
          return false;
      }
      
      //清除之前的token,实现单点登录
      Token::clear($user->id);
      
      //直接登录会员
      return $this->direct($user->id);
  }

按照上面的操作,单点登录就实现了


网站公告

今日签到

点亮在社区的每一天
去签到