首页 php框架 ThinkPHP thinkphp5框架API token身份验证功能【示例】

thinkphp5框架API token身份验证功能【示例】

Feb 13, 2021 am 10:10 AM
thinkphp5

下面由thinkphp教程栏目给大家介绍【示例】thinkphp5框架API token身份验证功能,希望对需要的朋友有所帮助!

thinkphp5框架API token身份验证功能【示例】

使用说明:登陆时生成token和刷新用的refresh_token,返回给客户端,客户端收到保存本地localStorage等,每次访问接口带上token,后端验证token存在并且一致后方可执行接下来的动作,假如不存在就返回token过期,客户端调用刷新接口传入token和refresh_token,服务器端进行验证,验证通过重新生成新的token保存数据库,返回给客户端客户端刷新本地token访问即可继续,当refresh_token验证失败就清除数据库token,过期时间等信息

简单的token生成函数(公共函数文件common)

function create_token($id,$out_time){
  return substr(md5($id.$out_time),5,26);
}
登录后复制

验证登陆方法(模型)

public function checkLogin($username,$passwd){
    $driver = self::field('driver_id,passwd')->where('zhanghao',$username)->whereOr('phone',$username)->find();
    if (empty($driver)){
      $this->error = '账号不存在';
      return false;
    }
    if ($driver['passwd'] != md5($passwd)){
      $this->error = "密码不正确";
      return false;
    }
    //$out_time = strtotime('+ 1 days');
    $out_time = strtotime('+ 1 minutes');
    $token = create_token($driver['driver_id'],$out_time);
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      $this->error = '登陆失败';
      return false;
    }
    $refresh_token_out_time = strtotime('+ 5 days');
    $refresh_token = create_token($driver['driver_id'],$refresh_token_out_time);
    Cache::set("token",$token,60);
    Cache::set("driver_id",$driver['driver_id'],$refresh_token_out_time);//设置ID的过期时间和更新token的token时间一样用于更新的时候获取用户信息
    Cache::set('refresh_token',$refresh_token,$refresh_token_out_time);
    return ['token'=>$token,'refresh_token'=>$refresh_token,'in_expire'=>$out_time];
}
登录后复制

token刷新方法(模型)

public function refreshToken($refresh_token,$token){
    if (!isset(Cache::get('refresh_token')) or Cache::get('refresh_token')!=$refresh_token){
      $this->error = '刷新token失败';
      return false;
    }
    $cache_driver_id = Cache::get('driver_id');
    $driver = self::field('driver_id,passwd')->where('driver_id',$cache_driver_id)->where('token',$token)->find();
    if (empty($driver)){
      $this->error = '参数错误';
      return false;
    }
    $out_time = strtotime('+ 1 days');//新的过期时间
    $token = create_token($driver['driver_id'],$out_time);//更新token
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      Cache::clear($token);
      $this->error = '刷新失败';
      return false;
    }
    Cache::set("token",$token,864000);
    return ['token'=>$token,'in_expire'=>$out_time];
}
登录后复制

退出方法(模型)

public function logout($token,$refresh_token=''){
    $driver = self::field('driver_id,passwd')->where('token',$token)->find();
    self::save(['token'=>'','time_out'=>''],['token'=>$token]);
    Cache::clear('token');
    Cache::clear('refresh_token');
}
登录后复制

以上是thinkphp5框架API token身份验证功能【示例】的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

宝塔部署thinkphp5报错怎么办 宝塔部署thinkphp5报错怎么办 Dec 19, 2022 am 11:04 AM

宝塔部署thinkphp5报错的解决办法:1、打开宝塔服务器,安装php pathinfo扩展并启用;2、配置“.access”文件,内容为“RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]”;3、在网站管理里面,启用thinkphp的伪静态即可。

thinkphp5 url重写不行怎么办 thinkphp5 url重写不行怎么办 Dec 12, 2022 am 09:31 AM

thinkphp5 url重写不行的解决办法:1、查看httpd.conf配置文件中是否加载了mod_rewrite.so模块;2、将AllowOverride None中的None改为All;3、修改Apache配置文件.htaccess为“RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]”保存即可。

thinkphp5怎么获取请求过来的网址 thinkphp5怎么获取请求过来的网址 Dec 20, 2022 am 09:48 AM

thinkphp5获取请求网址的方法:1、使用“\think\Request”类的“$request = Request::instance();”方法获取当前的url信息;2、通过自带的助手函数“$request->url()”获取包含域名的完整URL地址。

怎么去除thinkphp5标题栏icon 怎么去除thinkphp5标题栏icon Dec 20, 2022 am 09:24 AM

去除thinkphp5标题栏icon的方法:1、找到thinkphp5框架public下的favicon.ico文件;2、删除该文件或者选择另一张图片命名改为favicon.ico,并替换原favicon.ico文件即可。

thinkphp5 post得不到值怎么办 thinkphp5 post得不到值怎么办 Dec 06, 2022 am 09:29 AM

thinkphp5 post得不到值是因为TP5是通过strpos函数在Header的content-type值中查找app/json字符串的,其解决办法就是设置Header的content-type值为app/json即可。

thinkphp5提示控制器不存在怎么办 thinkphp5提示控制器不存在怎么办 Dec 06, 2022 am 10:43 AM

thinkphp5提示控制器不存在的解决办法:1、检查对应的控制器里面的命名空间是否写对,修改为正确的命名空间;2、打开相应的tp文件,修改类名即可。

ThinkPHP5怎么查询昨天的数据 ThinkPHP5怎么查询昨天的数据 Dec 05, 2022 am 09:20 AM

ThinkPHP5查询昨天数据的方法:1、打开ThinkPHP5相关文件;2、通过表达式“db('table')->whereTime('c_time', 'yesterday')->select();”查询昨天的数据即可。

thinkphp5报错提示怎么设置 thinkphp5报错提示怎么设置 Dec 07, 2022 am 10:31 AM

thinkphp5设置报错提示的方法:1、进入项目根目录下的public文件夹,打开index.php入口文件;2、查看调试模式开关的注释;3、将“APP_DEBUG”常量的值调整为true即可展示错误信息提示。

See all articles