clystnet/laravel-jwt
Composer 安装命令:
composer require clystnet/laravel-jwt
包简介
A jwt package for reisssuing token purpose based on jwt-auth package. This is a fork of the unisharp/laravel-jwt package.
README 文档
README
This is a fork of the UniSharp/laravel-jwt package
Approach
If you pick Tymon JWTAuth as your jwt solution in your project, when you try to refresh your token, the package will blacklist your exchanged token (assume your blacklist feature is enabled). So when your client faces a concurrency use case, your request might be rejected because that request is sent before your app renews jwt token returned by server. This package caches the refreshed jwt token in a short period to ensure your client side can get correct response even if your request carries an old token in a concurrency case.
Installation
- Via Composer
composer require clystnet/laravel-jwt
- Add the Service Provider
PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider::class, Clystnet\JWT\JWTServiceProvider::class,
In Lumen please use
PHPOpenSourceSaver\JWTAuth\Providers\LumenServiceProvider::class,
Next, also in the app.php config file, under the aliases array, you may want to add the JWTAuth facade.
'JWTAuth' => 'PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth',
'JWTFactory' => 'PHPOpenSourceSaver\JWTAuth\Facades\JWTFactory'
Finally, you will want to publish the config using the following command:
php artisan vendor:publish --provider="PHPOpenSourceSaver\JWTAuth\Providers\JWTAuthServiceProvider"
php artisan vendor:publish --provider="Clystnet\JWT\JWTServiceProvider"
Don't forget to set a secret key in the config file!
$ php artisan jwt:secret
this will generate a new random key, which will be used to sign your tokens.
And you're done!
Usage
Open your config/auth.php config file and in place of driver under any of your guards, just add the jwt-auth as your driver and you're all set.
Make sure you also set provider for the guard to communicate with your database.
Setup Guard Driver
// config/auth.php 'guards' => [ 'api' => [ 'driver' => 'jwt-auth', 'provider' => 'users' ], // ... ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ],
Middleware Usage
Middleware protecting the route:
Route::get('api/content', ['middleware' => 'laravel.jwt', 'uses' => 'ContentController@content']);
Middleware protecting the controller:
<?php namespace App\Http\Controllers; class ContentController extends Controller { public function __construct() { $this->middleware('laravel.jwt'); } }
This middleware will automatically refresh jwt token if the existing one has been expired. The new refreshed jwt token will be carried to the response header:
Ahthorization. The client side needs to replace your expired jwt token with the new one.
Note: The above example assumes you've setup a guard with the name api whose driver is jwt-auth in your config/auth.php file as explained in "Setup Guard Driver" section above.
The following usage examples assume you've setup your default auth guard to the one which uses the
jwt-authdriver.You can also explicitly define the guard before making calls to any of methods by just prefixing it with
Auth::guard('api').Example:
Auth::guard('api')->user()
Attempt To Authenticate And Return Token
// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests. $token = Auth::attempt(['email' => 'user@domain.com', 'password' => '123456']);
Authenticate Once By ID
if(Auth::onceUsingId(1)) { // Do something with the authenticated user }
Authenticate Once By Credentials
if(Auth::once(['email' => 'user@domain.com', 'password' => '123456'])) { // Do something with the authenticated user }
Validate Credentials
if(Auth::validate(['email' => 'user@domain.com', 'password' => '123456'])) { // Credentials are valid }
Check User is Authenticated
if(Auth::check()) { // User is authenticated }
Check User is a Guest
if(Auth::guest()) { // Welcome guests! }
Logout Authenticated User
Auth::logout(); // This will invalidate the current token and unset user/token values.
Generate JWT Auth Token By ID
$token = Auth::generateTokenById(1); echo $token;
Get Authenticated User
Once the user is authenticated via a middleware, You can access its details by doing:
$user = Auth::user();
You can also manually access user info using the token itself:
$user = Auth::setToken('YourJWTAuthToken')->user();
Get Authenticated User's ID
$userId = Auth::id();
Refresh Expired Token
Though it's recommended you refresh using the middlewares provided with the package, but if you'd like, You can also do it manually with this method.
Refresh expired token passed in request:
$token = Auth::refresh();
Refresh passed expired token:
Auth::setToken('ExpiredToken')->refresh();
Invalidate Token
Invalidate token passed in request:
$forceForever = false; Auth::invalidate($forceForever);
Invalidate token by setting one manually:
$forceForever = false; Auth::setToken('TokenToInvalidate')->invalidate($forceForever);
Get Token
$token = Auth::getToken(); // Returns current token passed in request.
Get Token Payload
This method will decode the token and return its raw payload.
Get Payload for the token passed in request:
$payload = Auth::getPayload();
Get Payload for the given token manually:
$payload = Auth::setToken('TokenToGetPayload')->getPayload();
clystnet/laravel-jwt 适用场景与选型建议
clystnet/laravel-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「token」 「jwt」 「concurrency」 「reissue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 clystnet/laravel-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 clystnet/laravel-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 clystnet/laravel-jwt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple library to decode and parse Apple Sign In client tokens.
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
JWT API authentication driver (Guard) for Laravel.
JSON Web Token Authentication for Laravel and Lumen
Laravel JWT auth service package
统计信息
- 总下载量: 77
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-25