jerrygaoyang/laravel-jwt
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' => ''
]);
}
jerrygaoyang/laravel-jwt 适用场景与选型建议
jerrygaoyang/laravel-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.13k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jerrygaoyang/laravel-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jerrygaoyang/laravel-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 45.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-14