fastadmin加密生成token

ops/2025/2/4 6:54:10/

安装git

sudo yum install git

在项目中安装 firebase/php-jwt

composer require firebase/php-jwt

 注意:PHP7.4以上,安装fileinfo

如果还有问题在PHP配置里禁止:

;disable_functions = passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv

后端代码生成token

$payload = array("openId" => $openid,"iat" => time(), // 签发时间"exp" => time() + (60 * 60) // 过期时间,这里设置为1小时
);$token = JWT::encode($payload, $sessionKey, 'HS256');
echo $token;

但这里有问题是,非32位的token


于是有了以下的token生成,两个方法:

$token = JWT::encode($payload, $sessionKey, 'HS256'); 

$token = substr(hash('sha256', uniqid(rand(), true)), 0, 32); 


最后使用思路逻辑是:

生成一个随机32位token,同时存储创建时间和过期时间,

 ===在登陆时,拿缓存中的token对比是否过期,如果过期就重新登陆

 ===重新登陆,用session_key新生成openid,因为openid是不变的,用它找到对应的user_id然后就可以去更新其用户信息和token表的信息,是直接更新,不是重新创建

后端代码:(没有缓存token时,直接用注册信息验证,更新token登陆)

/** 注册/登陆用户信息* 如果有$token就登陆如果没有$token就注册用户信息*/public function getUserInfo(){// 获取得到avatarUrl、nickName、openId,进行存储$data = $this -> app -> encryptor->decryptData($this -> post['session_key'], $this -> post['iv'], $this -> post['encryptedData']);$userModel = new HcdrspUser();$result = $userModel->where('openid', $data['openId'])->find();if(!$result){$userModel->openid = $data['openId'];$userModel->avatarUrl = $data['avatarUrl'];$userModel->name = $data['nickName'];$userModel->status = 'normal';// 创建时间$userModel->createtime = time();$userModel->save();}// 对user_token表处理$user_id = $result['id'];$token = bin2hex(random_bytes(16));$expireTime = time() + (60 * 60 * 12); // 12小时过期时间$createtime = time();$userToken = new UserToken();$token_result = $userToken->where('user_id', $user_id)->find();if($token_result){$userToken -> where('user_id', $user_id) -> update(['token' => $token, 'expiretime' => $expireTime]);$this->success('登陆成功', ['token' => $token,'userinfo' => $result]);}else{$userToken -> token = $token;$userToken -> expiretime = $expireTime;$userToken -> user_id = $user_id;$userToken -> createtime = $createtime;$userToken -> save();$this->success('欢迎新用户', ['token' => $token,'userinfo' => $result]);}}

后端代码:(有缓存token时,验证token并更新token登陆)

/** 登陆验证* 验证token*/public function login(){$token = $this ->request ->post('token');if ($token){$token_result = UserToken::where('token', $token)->find();if($token_result){if ($token_result['expiretime'] < time()){$this->error('登陆过期', $token);}elseif ($token_result['expiretime'] > time()){$user_id = $token_result['user_id'];$token = bin2hex(random_bytes(16));$expireTime = time() + (60 * 60 * 12); // 12小时过期时间$userToken = new UserToken();$userToken -> where('user_id', $user_id) -> update(['token' => $token, 'expiretime' => $expireTime]);$userModel = new HcdrspUser();$result = $userModel->where('id', $user_id)->find();$this->success('登陆成功', $result);}}else{$this->error('登陆失败', $token);}}else{$this->error('请先登陆', $token);}}


http://www.ppmy.cn/ops/155503.html

相关文章

【贪心算法篇】:“贪心”之旅--算法练习题中的智慧与策略(一)

✨感谢您阅读本篇文章&#xff0c;文章内容是个人学习笔记的整理&#xff0c;如果哪里有误的话还请您指正噢✨ ✨ 个人主页&#xff1a;余辉zmh–CSDN博客 ✨ 文章所属专栏&#xff1a;贪心算法篇–CSDN博客 文章目录 一.贪心算法1.什么是贪心算法2.贪心算法的特点 二.例题1.柠…

Linux-CentOS的yum源

1、什么是yum yum是CentOS的软件仓库管理工具。 2、yum的仓库 2.1、yum的远程仓库源 2.1.1、国内仓库 国内较知名的网络源(aliyun源&#xff0c;163源&#xff0c;sohu源&#xff0c;知名大学开源镜像等) 阿里源:https://opsx.alibaba.com/mirror 网易源:http://mirrors.1…

mybatis(78/134)

前天学了很多&#xff0c;关于java的反射机制&#xff0c;其实跳过了new对象&#xff0c;然后底层生成了字节码&#xff0c;创建了对应的编码。手搓了一遍源码&#xff0c;还是比较复杂的。 <?xml version"1.0" encoding"UTF-8" ?> <!DOCTYPE …

软件测试 - 概念篇

目录 1. 需求 1.1 用户需求 1.2 软件需求 2. 开发模型 2.1 软件的生命周期 2.2 常见开发模型 2.2.1 瀑布模型 2.2.2 螺旋模型 1. 需求 对于软件开发而言, 需求分为以下两种: 用户需求软件需求 1.1 用户需求 用户需求, 就是用户提出的需求, 没有经过合理的评估, 通常…

gdb 调试多进程中多线程的方法

示例代码 首先&#xff0c;给出一个简单的示例程序&#xff0c;演示如何使用 fork 创建多个子进程并且每个进程内部创建多个线程。 示例代码 (main.cpp) #include <iostream> #include <thread> #include <vector> #include <unistd.h> #include <…

【游戏设计原理】98 - 时间膨胀

从上文中&#xff0c;我们可以得到以下几个启示&#xff1a; 游戏设计的核心目标是让玩家感到“时间飞逝” 游戏的成功与否&#xff0c;往往取决于玩家的沉浸感。如果玩家能够完全投入游戏并感受到时间飞逝&#xff0c;说明游戏设计在玩法、挑战、叙事等方面达到了吸引人的平衡…

【漫话机器学习系列】078.如何选择隐藏单元激活函数(How To Choose Hidden Unit Activation Functions)

选择隐藏单元激活函数是神经网络设计中的一个重要步骤&#xff0c;它直接影响到模型的学习能力和训练效果。不同的激活函数具有不同的性质和适用场景&#xff0c;因此在选择时需要根据模型的需求和问题的特性来决定。以下是一些常见的激活函数及其选择依据&#xff1a; 1. Sig…

人工智能学习(四)之机器学习基本概念

机器学习基本概念详细解析&#xff1a;从生活实例轻松入门 在当今数字化时代&#xff0c;机器学习作为人工智能领域的核心技术之一&#xff0c;正深刻地改变着我们的生活和工作方式。从智能语音助手到图像识别系统&#xff0c;从个性化推荐引擎到自动驾驶汽车&#xff0c;机器…