1.引入扩展包
composer require firebase/php-jwt
<?php
namespace app\business;
use Firebase\JWT\Key;
use think\Exception;
class Jwt
{
public static function createJwt($userId = 'zq')
{
$key = md5('zq8876!@!'); //jwt的签发密钥,验证token的时候需要用到
$time = time(); //签发时间
$expire = $time + 14400; //过期时间
$token = array(
"user_id" => $userId,
"iss" => "http://www.najingquan.com/",//签发组织
"aud" => "zhangqi", //签发作者
"iat" => $time,
"nbf" => $time,
"exp" => $expire
);
// 生成token
$jwt = \Firebase\JWT\JWT::encode($token, $key,'HS256');
return $jwt;
}
public static function verifyJwt($jwt = '')
{
$key = md5('zq8876!@!');