美文网首页
2019-12-13 Laravel 6 安装Oauth Pas

2019-12-13 Laravel 6 安装Oauth Pas

作者: 垄山小站 | 来源:发表于2019-12-16 17:29 被阅读0次

    1. 系统环境

    Ubuntu 16.04.6 + Apache 2.2  + MySQL 5.7.28

    PHP 7.2.25 + Composer

    2. 创建 Laravel 6 项目

    composer create-project --prefer-dist laravel/laravel project-name '6.5.*'

    (注:不指定版本号 6.5.*,就会安装 composer 上最新版本,项目会创建在 composer 运行所在目录)

    cd project-name

    sudo chmod -R 777 storage/ bootstrap/ database/migrations/

    修改 .env,连接上 MySQL

    3. 安装 passport

    composer require laravel/passport

    php artisan migrate

    php artisan passport:install    // 此命令在oauth_clients 表创建2个client

    4. 配置 passport

    修改 config/auth.php 文件,将 api 的 driver 改为 passport

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

    修改 app/Providers/AuthServiceProvider.php, 在 boot 方法中增加 Passport::routes() ...

    <?php

        namespace App\Providers;

        use Illuminate\Support\Facades\Gate;
        use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

        use Illuminate\Support\Carbon;
        use Laravel\Passport\Passport;


        class AuthServiceProvider extends ServiceProvider
        {

            ...

            public function boot()
            {
                $this->registerPolicies();
               
                Passport::routes();
                Passport::tokensExpireIn(Carbon::now()->addHour(2));
                Passport::refreshTokensExpireIn(Carbon::now()->addDay(1));

            }

            ...

    6. 安装 ui 和 Auth

    composer require laravel/ui

    php artisan ui bootstrap --auth   // Laravel 6 命令 php artisan make:auth 已被移除

    php artisan serve --host=192.168.0.4 --port=8000  // 根据自己的测试环境调整 host 和 port

    http://192.168.0.4:8000/register

    创建用户 test 成功,密码:12345678

    7. 测试 grant_type = password 模式

    8. 测试 grant_type = authorization_code 模式

       1)  创建一个新的客户端绑定用户 test

         php artisan passport:client

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

     What should we name the client?:
     > Laravel Passport Authorization Code Client

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

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

    2) 修改 routes/web.php

    添加如下代码

    use Illuminate\Http\Request;

    ...

    Route::get('/redirect', function () {

      $query = http_build_query([

           'client_id' => '3',

           'redirect_uri' => 'http://192.168.0.4:8000/auth/callback',

           'response_type' => 'code',

           'scope' => '',

      ]);

      return redirect('http://192.168.0.4:8000/oauth/authorize?' . $query);

    });

    Route::get('/auth/callback', function (Request $request) {

       return json_encode(array('code' => $request->code));

    });

    3)访问 http://192.168.0.4:8000/redirect,  自动跳转到 Authorize 页面

    点击 Authorize 按钮, 显示 code

    4)用Postman, 取 tokens

    相关文章

      网友评论

          本文标题:2019-12-13 Laravel 6 安装Oauth Pas

          本文链接:https://www.haomeiwen.com/subject/trsfnctx.html