'webman', // 签发者 'aud' => 'client', // 接收方 'iat' => time(), // 签发时间 'exp' => time() + $config['expire'], // 过期时间 'sub' => $userId // 用户标识 ]; return JWT::encode($payload, $config['secret'], $config['algorithm']); } /** * 验证并解析 Token * @param string $token * @return object * @throws BusinessException */ public static function verifyToken(string $token) { $config = config('jwt'); try { return JWT::decode($token, new Key($config['secret'], $config['algorithm'])); } catch (ExpiredException $e) { throw new BusinessException('Token 已过期', 401); } catch (SignatureInvalidException $e) { throw new BusinessException('Token 无效', 401); } catch (\Exception $e) { throw new BusinessException('鉴权失败', 401); } } }