码迷,mamicode.com
首页 > Windows程序 > 详细

postman 设置api_token,测试数据等操作

时间:2017-03-13 18:59:47      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:hba   用户   signed   ttl   ret   个数   global   invalid   throttle   

在postman的环境变量中先设置好一个用户的token(事先你的数据库里面要有用户的这个数据),如下图:

技术分享

然后在api请求的url 的head头部添加对应的token键名,value值用花括号{{token}}包住,就会自动获取其值。

技术分享

 

1、routes.php文件设置中间件路由,将你的控制器放在里面

    Route::group([‘middleware‘ => ‘auth.api‘], function () {

        Route::controllers([

            ‘check_lists‘ => ‘CheckListsController‘,
        ]);
    });

 

中间件middleware文件目录下创建AuthenticateApi.php

<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class AuthenticateApi
{
    public function handle($request, Closure $next)
    {

        $token = $request->header(‘Token‘);
        if (! $token) {
            return \JSend::error(‘Missing Api Token.‘, 401);
        }

        $user = User::where(‘token‘, $token)->first();
        if (! $user) {
            return \JSend::error(‘Invalid Api Token.‘, 401);
        }

        Auth::setUser($user);

        return $next($request);
    }
}

在Kernel.php文件里面注册该中间件。

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application‘s global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [

    ];

    /**
     * The application‘s route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        ‘web‘ => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        ‘api‘ => [
            // ‘throttle:60,1‘,
        ],
    ];

    /**
     * The application‘s route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
    
        ‘auth.api‘   => \App\Http\Middleware\AuthenticateApi::class,
    
        ‘auth.basic‘ => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        ‘can‘ => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        ‘guest‘ => \App\Http\Middleware\RedirectIfAuthenticated::class,
        ‘throttle‘ => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

接着,在url请求的控制器里面验证下时候请求成功

<?php

namespace App\Http\Controllers\Api;

use App\Models\CheckList;
use App\Models\User;
use App\Models\Good;
use App\Models\GoodCate;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class CheckListsController extends Controller
{

    //添加检查清单
    public function postAddList(Request $request)
    {
        dd(auth()->user()->id);

成功获取到该token对应的用户的ID;

技术分享

 

postman 设置api_token,测试数据等操作

标签:hba   用户   signed   ttl   ret   个数   global   invalid   throttle   

原文地址:http://www.cnblogs.com/smallyi/p/6544281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!