randomstate/laravel-auth-jwt
Composer 安装命令:
composer require randomstate/laravel-auth-jwt
包简介
JWT issuing and verification strategy for randomstate/laravel-auth
README 文档
README
This is a JWT authentication strategy for randomstate/laravel-auth.
It serves both the issuing and the authentication of JWTs.
Usage
Register with Auth Manager: Follow strategy service provider example here: https://github.com/randomstate/laravel-auth
Configuration
You should configure your token issuer (Issuer::class) so that the appropriate standard claims are made (iat, aud etc) depending on your needs.
<?php use \RandomState\LaravelAuth\Strategies\JwtStrategy; use \RandomState\LaravelAuth\Strategies\Jwt\Issuer; use \Carbon\CarbonInterval; class MyServiceProvider extends \Illuminate\Support\ServiceProvider { public function register() { $this->app->resolving(\Illuminate\Auth\AuthManager::class, function($manager) { $manager->register('jwt', $this->app->make(JwtStrategy::class)); }); $this->app->resolving(JwtStrategy::class, function($strategy) { $strategy->convertUsing(function(\RandomState\LaravelAuth\Strategies\JwtUser $user) { return User::find($user->id()); // assuming you are using Eloquent }); }); $this->app->bind(Issuer::class, function() { $issuer = new Issuer(); $issuer ->withIssuer('my_app') // chain and build your configuration ->withAudience('my_app') ->withExpirationWindow(CarbonInterval::minutes(60)) ->signTokens(new \Lcobucci\JWT\Signer\Rsa\Sha256(), config('auth.jwt_signing_key')) // your private RSA key in this example ; }); } }
Issuing a Token
This package automatically resolves your Issuer::class configuration out of the Laravel container. This means that any tokens issued can be checked without worrying if you have configured everything correctly - as long as you don't change the way laravel binds the Issuer::class between issuing a token and consuming it, you can rely on it being consistent.
Typically you will want to use a specific login route to authenticate a user via username and password. You should simply perform any login logic (usually handled out of the box with Laravel) and then issue and return a JWT token as so:
<?php use \Illuminate\Http\Request; use \App\Http\Controllers\Controller; use \RandomState\LaravelAuth\Strategies\Jwt\Issuer; use \Illuminate\Support\Facades\Auth; class LoginController extends Controller { public function login(Request $request, Issuer $issuer) { $token = $issuer->issue(Auth::user()->getAuthIdentifier()); return response($token); } }
Authenticating via JWT
You may supply your JWT token as an Authorization: Bearer {token} header or as a token request parameter in your request.
This will automatically be pulled out when using the LaravelAuth authenticate middleware and authenticated.
randomstate/laravel-auth-jwt 适用场景与选型建议
randomstate/laravel-auth-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 randomstate/laravel-auth-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 randomstate/laravel-auth-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-09