imemento / guard-laravel
我们为 Laravel 定制的守卫
v8.1.0
2021-07-25 10:59 UTC
Requires
- php: ^8.0
- imemento/jwt: ~8.1.0
- imemento/sdk-auth: ~8.1.0
- laravel/framework: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-08-26 20:15:59 UTC
README
负责授权,并为用户和消费者设置角色和权限。依赖于 iMemento JWT 包来处理与 JWT 相关的任务。令牌的解密发生在 JWT 包中。
安装
composer require imemento/guard-laravel
该包使用服务发现。但如果需要,您可以将服务添加到 config/app.php
iMemento\Guard\Laravel\AuthServiceProvider::class,
在 config/auth.php
中添加一个使用 jwt 作为驱动程序的守卫
'api' => [ 'driver' => 'jwt', 'provider' => 'users', ],
在 config/auth.php
中添加一个使用 static 作为驱动程序的用户提供者。模型需要是 iMemento\SDK\Auth\User::class
的实例或其扩展。
'users' => [ 'driver' => 'static', 'model' => iMemento\SDK\Auth\User::class, ],
依赖
由于该包处理多个操作以实现预期结果,以下 .env
变量应正确定义
AUTH_KEY=
使用方法
要使用 JWT 守卫对 routes/api.php
文件中的所有路由,您只需将其添加到 app/Http/Kernel.php
中的 api
中间件组。
'api' => [ 'throttle:60,1', 'bindings', 'auth:api', #this ],
如果您的 API 公开了公开端点,那些应该由 JWT 守卫保护的具体分组
Route::group(['middleware' => 'auth:api'], function ()) { //... }
认证用户
一旦应用了守卫,应用将通过 auth()->user()
获取到认证用户。
以下字段被添加到当前用户中,并可用于应用程序的策略中。
{ "id": 13, "agency_id": 2, "roles": ["admin"], "consumer_roles": ["user"], "permissions": ["read","write"] }
字段 id、agency_id 可以是 null,roles 可以是空的。