jerrygaoyang/laravel-jwt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jerrygaoyang/laravel-jwt

最新稳定版本:v1.0.5

Composer 安装命令:

composer require jerrygaoyang/laravel-jwt

包简介

jwt auth for encode decode and token expire_in authenticate

README 文档

README

Installation

  • First: Require this package with composer using the following command

composer require jerrygaoyang/laravel-jwt

  • Second: add the service provider to the providers array in config/app.php

Jerry\JWT\Providers\JWTProvider::class

  • Last: publish jwt config to laravel config path

php artisan vendor:publish --provider="Jerry\JWT\Providers\JWTProvider"

Configuration

  • add the JWT middleware to the routeMiddleware array in app/Http/Kenel.php

'jwt' => \Jerry\JWT\Middleware\JWTMiddleware::class,

  • JWT config in config/jwt.php
<?php /** * Created by PhpStorm. * User: gaoyang * Date: 2017/11/14 * Time: 9:32 */ return [ "secret" => "PIe5T3xJWAMA95Uwf7pde7gmS7ZTiURg", //jwt SHA256 signature use the secret "expire_in" => 604800, //jwt expire_in the times(seconds) , default 604800(a week) ]; 

User Guide

encode and decode

use Jerry/JWT/JWT; $payload = [ "user_id" => 1 ]; $token = JWT::encode($payload); print_r($token); echo "<br>"; $payload = JWT::decode($token); print_r($payload); 

http api process for laravel  

  • First: http request must have header
{ "Authorization": "jwt PIe5T3xJWAMA95Uwf7pde7gmS7ZTiURg" } 
  • Second: laravel route use middleware jwt
Route::middleware(['jwt'])->group(function () { Route::get('/test2', 'TestController@test2'); }); 
  • Last: user request get jwt decode payload in your laravel controller
$payload = $request->get('jwt'); 

Exception (global for api)

you can copy below code to your Laravel app/Exceptions/handler.php render function;

it's easy to change the token exception for us;

it's easy to change the return data for api response.

of course, we should:

use Jerry\JWT\Exceptions\TokenFormatException;

use Jerry\JWT\Exceptions\TokenExpiredException;

use Jerry\JWT\Exceptions\TokenForwardException;

use Jerry\JWT\Exceptions\PayloadFormatException;

 if ($exception instanceof TokenFormatException) { return response()->json([ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), 'data' => '' ]); } if ($exception instanceof TokenExpiredException) { return response()->json([ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), 'data' => '' ]); } if ($exception instanceof TokenForwardException) { return response()->json([ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), 'data' => '' ]); } if ($exception instanceof PayloadFormatException) { return response()->json([ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), 'data' => '' ]); } 

统计信息

  • 总下载量: 41.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固