1、引入jwt包:
composer require firebase/php-jwt
2、封装jwt工具类如下:
<?php
namespace app\index\service;
use Firebase\JWT\JWT;
class JwtService {
const jwtKey = "Km4WkO4ofp4HFNb0"; //自定义jwt签发密钥
//生成token
public static function create($uid = 666) {
$time = time();
$token = [
"iss" => "", //签发组织
"aud" => "lws", //接收该JWT的一方
"iat" => $time, //签发时间
"nbf" => $time, //延后使用时间
"exp" => $time + 14400, //过期时间
"uid" => $uid //自定义校验数据
];
return JWT::encode($token, self::jwtKey);
}
//校验token
public static function verify($jwt = '') {
try {
JWT::$leeway = 60; //当前时间减去60,把时间留点余地
$decode