用 Laravel 搭建带 OAuth2 验证的 RESTful 服务

原文请看我的博客: 用Laravel搭建带Oauth2验证的RESTful服务

Github源码请看这里: Laravel-RESTful-Service

参考了 Laravel 5 token based Authentication (OAuth 2.0) & Dingo Wiki,但是原文中有一些bug,而且不适用最新的5.2版本,我的教程里解决了这些问题。

1.全新安装Laravel并配置好你的数据库,我在这里用的是mysql.

composer global require "laravel/installer"
laravel new restful

2.修改 composer.json 并在命令行运行 composer update 添加两个包.

    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*",
        //下面这两个是新添加的,注意版本号
        "dingo/api": "1.0.*dev",
        "lucadegasperi/oauth2-server-laravel": "5.1.*"
    }

3.在 config/app.php 文件里添加新的 providers.

<?php
    'providers' => [

        //Add bottom lines to your providers array.
        /**
         * Customized Service Providers...
         */
        Dingo\Api\Provider\LaravelServiceProvider::class,
        LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
        LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,

    ],

aliases 里添加下面的内容:

<?php
    'aliases' => [

        //Add bottom lines to your aliases array.
        'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,

    ],

4.在你的 app/Http/Kernel.php 文件里添加新的 $middleware & $routeMiddleware.

<?php
    protected $middleware = [
        //Add bottom lines to your $middleware array.
        \LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
    ];
    //
    protected $routeMiddleware = [
        //Add bottom lines to your $routeMiddleware array.
        'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class,
        'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class,
        'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class,
        'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class,
    ];

5.在你的项目里运行 php artisan vendor:publish & php artisan migrate .

添加这些配置项在 .env 文件里,具体含义可以查看Dingo Wiki:

API_STANDARDS_TREE=x
API_SUBTYPE=rest
API_NAME=REST
API_PREFIX=api
API_VERSION=v1
API_CONDITIONAL_REQUEST=true
API_STRICT=false
API_DEBUG=true
API_DEFAULT_FORMAT=json

配置你的 app\config\oauth2.php 文件:

<?php
    //Modify the $grant_types as follow.
    'grant_types' => [
            'password' => [
             'class' => 'League\OAuth2\Server\Grant\PasswordGrant',
             'access_token_ttl' => 604800,

             // the code to run in order to verify the user's identity
             'callback' => 'App\Http\Controllers\VerifyController@verify',
             ],
        ],

6.在 routes.php 文件添加我们需要的路由.


<?php
//Add the following lines to your routes.php

/**
 * OAuth
 */

//Get access_token
Route::post('oauth/access_token', function() {
 return Response::json(Authorizer::issueAccessToken());
});

//Create a test user, you don't need this if you already have.
Route::get('/register',function(){$user = new App\User();
 $user->name="tester";
 $user->email="test@test.com";
 $user->password = \Illuminate\Support\Facades\Hash::make("password");
 $user->save();
});

/**
 * Api
 */
$api = app('Dingo\Api\Routing\Router');

//Show user info via restful service.
$api->version('v1', ['namespace' => 'App\Http\Controllers'], function ($api) {
    $api->get('users', 'UsersController@index');
    $api->get('users/{id}', 'UsersController@show');
});

//Just a test with auth check.
$api->version('v1', ['middleware' => 'api.auth'] , function ($api) {
    $api->get('time', function () {
        return ['now' => microtime(), 'date' => date('Y-M-D',time())];
    });
});

7.在数据库的 oauth_client 表里添加一条 client 数据用来测试. 例如phphub就是github API的一个client

INSERT INTO `oauth_clients` (`id`, `secret`, `name`, `created_at`, `updated_at`) VALUES
(‘f3d259ddd3ed8ff3843839b’, ‘4c7f6f8fa93d59c45502c0ae8c4a95b’, ‘Main website’, ‘2015–05–12 21:00:00’, ‘0000–00–00 00:00:00’);

8.编写你的 API Controller.

随便叫什么 Book,Post,User 都好,这是一个举例:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UsersController extends Controller
{

    public function index()
    {
        return User::all();
    }

    public function show($id)
    {
        return User::findOrFail($id);
    }
}

9.开始测试吧!

其实已经初步完工了,接下来我们要测试刚才配置好的服务,一般会用到一个Chrome应用 PostMan 来模拟请求你的服务器,当然你也可以用自己的办法.

GET from Server
Oauth2
Token test

本帖已被设为精华帖!
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 30

@discountry 搭起来了,如果token失效了怎么刷新token或者重新生成token?

7年前 评论

TokenMismatchException 楼主我碰到了这个问题,不知道应该怎么解决,麻烦看下,谢谢

7年前 评论
leililei

现在laravel官方有passport了,也是个不错的选择

5年前 评论

file
你好,我遇到了这个问题。请问如何处理??另外laravel版本如果5.7.*版本,composer.js如何添加??

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!