API key Authentication for Laravel and Lumen.
composer require leonis/apikey-auth
-
在
config/app.php
注册 ServiceProvider (Laravel 5.5 及以上无需手动注册)'providers' => [ // ... Leonis\ApiKeyAuth\Providers\ServiceProvider::class ],
-
创建配置文件:
php artisan vendor:publish --provider="Leonis\ApiKeyAuth\ApiKeyAuthServiceProvider"
-
运行数据库迁移
php artisan migrate
-
在
bootstrap/app.php
注册 ServiceProvider$app->register(Leonis\ApiKeyAuth\Providers\ServiceProvider::class);
-
复制配置和数据库迁移文件
将
vendor/leonis/apikey-auth/config/config.php
拷贝到config
目录下,并将文件名改成api_key.php
; 将vendor/leonis/apikey-auth/database/migrations
目录中的文件拷贝到database/migrations
目录下。 -
运行数据库迁移
php artisan migrate
-
修改
auth.php
配置文件中的guards
:'guards' => [ // ... 'api' => [ 'driver' => 'api_key', 'provider' => 'users', ], ],
-
为用户生成 Api Key :
ApiKey::fromUser(User::first()); ApiKey::fromId($user->id);
-
请求 API :
TODO