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:{