1.引入php-jwt包
composer require firebase/php-jwt
2.代码
控制器文件:app\business\Jwt.php
<?php
namespace app\busines;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class JWT
{
public function add(){
//根据需要修改成你的密钥
$key = "example_key";
//加密数据,可以自定义删减
$payload = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
"nbf" => 1357000000,
"uid" => $uid
);
/**
* IMPORTANT:
* You must specify supported algorithms for your application. See
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
* for a list of spec-compliant algorithms.
*/
$jwt = JWT::encode($payload, $key, 'HS256');
return $jwt;
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
//可不用
print_r($decoded);
$decoded_array = (array) $decoded;
}
}
在微信小程序中使用jwt--token
基于token(令牌)的用户认证步骤:
1、用户输入其登录信息
2、服务器验证信息是否正确,并返回已签名的token
3、token储在客户端,例如存在local storage或cookie中(小程序存在缓存中)
4、之后的HTTP请求都将token添加到请求头里
//从缓存中取出token
const token = wx.getStorageSync('token')
wx.request({
url: '',
data:{

这篇博客介绍了如何在ThinkPHP6中集成JWT(JSON Web Tokens)进行用户身份验证。主要内容包括引入php-jwt库,创建JWT控制器,阐述基于token的用户认证流程,讲解在微信小程序中添加token的方法,并提供了ThinkPHP6中使用JWT的中间件验证、修改.htaccess文件以处理Authorization字段,以及前端存储和销毁token的步骤。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



