lameck / manuser
"为 Laravel 管理用户"
1.0.0
2018-03-25 22:44 UTC
Requires
- php: ^5.5.9 || ^7.0
- illuminate/auth: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*
- illuminate/contracts: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*
- illuminate/http: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*
- illuminate/support: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*
- tymon/jwt-auth: dev-develop
This package is not auto-updated.
Last update: 2024-10-02 04:27:08 UTC
README
打包管理用户 JWT
打包器有助于优化编程工作。每个项目常见的功能可以被重用。为了避免总是编写相同的代码,打包器可以帮助您加快这个过程,让您专注于真正需要做的事情。简而言之,这是一种优化流程的方法。
此打包器旨在优化使用 JWT 进行身份验证的 RestFull 路由、控制器和中间件。
签名:Packagerlist
安装
提供者:app/config/app.php
... Lameck\Manuser\ManuserServiceProvider::class, Tymon\JWTAuth\Providers\LaravelServiceProvider::class
别名:app/config/app.php
'JWTAuth' => Tymon\JWTAuthFacades\JWTAuth::class, 'JWTFactory' => Tymon\JWTAuthFacades\JWTFactory::class
内核:app/http/kernel.php
注释行
//\App\Http\Middleware\VerifyCsrfToken::class,在路由中间件中添加
'jwt.auth' => Tymon\JWTAuth\MiddlewareGetUserFromToken::class,
'jwt.refresh' => TymonJWTAuth\MiddlewareRefreshToken::class
注释
//'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,添加
'throttle' => \Lameck\Manuser\ThrottleRequestsMiddleware::class,
用户模型
在类中添加
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject{...}
public function getJWTCustomClaims(): array {
return [];
}
public function getJWTIdentifier(){
return $this->getKey();
}
如果服务器正在运行,请停止它并清除缓存。之后,重新启动
php artisan cache:clear;php artisan serve
如果不这样做,可能会出现以下错误消息:"访问拒绝"
示例
创建一个数据库并加载一个迁移文件
php artisan migrate
php artisan make:seeder UsersTableSeeder
DB::table('users')->delete();
$users = array(
['name' => 'Jerry Cantrell', 'email' => 'jerry@gmail.com', 'password' => Hash::make('secret')],
['name' => 'Ozzy Osbuorne', 'email' => 'ozzy@me.io', 'password' => Hash::make('secret')],
['name' => 'Leney Stanley', 'email' => 'leney@me.io', 'password' => Hash::make('secret')],
['name' => 'Kurtney Love', 'email' => 'kurtney@me.io', 'password' => Hash::make('secret')],
);
DB::table('users')->insert($users);
php artisan db:seed
php artisan jwt:secret
使用 Postman 测试 API。
POST
localhost:8000/manuser/authenticate?email=jerry@gmail.com&password=secret
GET
localhost:8000/manuser/users?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvbWFudXNlclwvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIyMDIwNTc5LCJleHAiOjE1MjIwMjQxNzksIm5iZiI6MTUyMjAyMDU3OSwianRpIjoidzA0YnRpejU3Q1NZRjRuZyIsInN1YiI6MSwicHJ2IjoiODdlMGFmMWVmOWZkMTU4MTJmZGVjOTcxNTNhMTRlMGIwNDc1NDZhYSJ9.xMWZb27t-VgyE5BbR0N5l1Iyf4Y4n5QBF-IuBA_CJgQ
简而言之,此打包器是使用 JWT 通过 RestFull API 自动化的工具 - 我们不再使用 VerifyCsrfToken 本地令牌 - 主要用于跨站脚本,我们使用 JWT。要使用它,需要进行手动配置 - 对于熟悉 Laravel 框架的资深开发者来说是常见的。
我将在 wiki 上提供更多详细信息。
细节
默认请求次数为 1,1(每分钟 1 次请求 - throttle:1,1)。要更改此值,请访问 vendor/lameck/manuser/route.php。第一个值是请求次数,第二个值是下一次请求之前的持续时间。
Route::group(['prefix' => 'manuser'], function() { Route::post('authenticate', '\Lameck\Manuser\ManuserController@authenticate'); Route::middleware('jwt.auth','throttle:1,1')->get('users','\Lameck\Manuser\ManuserController@users'); });