Laravel RESTful API

Installation

首先使用 composer 安装 Laravel 5.7。

composer create-project --prefer-dist laravel/laravel laravel-passport

然后运行如下命令启动内置 web 服务器。

php artisan serve

打开浏览器访问 http://localhost:8000/ 就能看到 Laravel 欢迎页面。

Authentication

安装完成之后,先修改 .env 文件中的数据库配置。

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

然后在命令行中运行下面两条命令。

php artisan make:auth
php artisan migrate

打开浏览器访问 http://localhost:8000/,你就会发现右上角多了 LOGIN 和 REGISTER 两个链接。

登录页面:http://localhost:8000/login

注册页面:http://localhost:8000/register

这一步我们注册了一个用户 Victor Tang。

mysql> select * from users;
+----+-------------+----------------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| id | name        | email                | email_verified_at | password                                                     | remember_token | created_at          | updated_at          |
+----+-------------+----------------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
|  1 | Victor Tang | tanghengzhi@live.com | NULL              | $2y$10$6skKXJPSjMWUHEsNVcVAeuxCgrUTf.JPMIfTpRyp723BGC1jZhCSW | NULL           | 2019-01-07 13:33:07 | 2019-01-07 13:33:07 |
+----+-------------+----------------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
1 row in set (0.00 sec)

API Authentication (Passport)

首先使用 composer 安装 Passport。

composer require laravel/passport

然后执行下面的命令创建需要的表。

php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2016_06_01_000001_create_oauth_auth_codes_table
Migrated:  2016_06_01_000001_create_oauth_auth_codes_table
Migrating: 2016_06_01_000002_create_oauth_access_tokens_table
Migrated:  2016_06_01_000002_create_oauth_access_tokens_table
Migrating: 2016_06_01_000003_create_oauth_refresh_tokens_table
Migrated:  2016_06_01_000003_create_oauth_refresh_tokens_table
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated:  2016_06_01_000004_create_oauth_clients_table
Migrating: 2016_06_01_000005_create_oauth_personal_access_clients_table
Migrated:  2016_06_01_000005_create_oauth_personal_access_clients_table

执行下面的命令生成 Encryption keys 并创建 Personal access client 和 Password grant client。

php artisan passport:install
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: d0m5V3kRf8e4ZnmOtivHILNDSp3AouxkScXp0Ikp
Password grant client created successfully.
Client ID: 2
Client secret: Jksb3tYCbta0Y4pD4mO7lwUPyAFpj2oo9IEpfQZG

然后在 App\User model 中添加 Laravel\Passport\HasApiTokens trait。

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
}

在 AuthServiceProvider 的 boot() 方法中调用 Passport::routes() 方法。

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Passport::routes();
}

最后修改 config/auth.php 配置文件中的 api 认证方式为 passport。

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Authorization Code Grant

首先创建 Passport Client。

php artisan passport:client
 Which user ID should the client be assigned to?:
 > 1

 What should we name the client?:
 > tanghengzhi

 Where should we redirect the request after authorization? [http://localhost/auth/callback]:
 > http://127.0.0.1:8000/callback

New client created successfully.
Client ID: 3
Client secret: ArBmsXOLFwm39A23L29banA1PS3jd7doo9dqDnKX

然后获取 Access Token。

Authorization Code Grant

Implicit Grant

首先启用 Implicit Grant。

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Passport::routes();

    Passport::enableImplicitGrant();
}

然后获取 Access Token。

Implicit Grant

Password Credentials Grant

首先创建 passport client。

php artisan passport:client --password
What should we name the password grant client? [Laravel Password Grant Client]:
 > 

Password grant client created successfully.
Client ID: 4
Client secret: AcwwOugmcdk9nlJlnazQxfOjwSNHQmqoFMBHe9Zg

然后获取 Access Token。

Password Credentials Grant

Client Credentials Grant

获取 Access Token。

Client Credentials Grant

参考:

https://laravel.com/docs/5.7
https://laravel.com/docs/5.7/authentication
https://laravel.com/docs/5.7/passport

 657 total views,  4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *