Swagger Authorization:bearer <token>

1、添加如下代码

   /**
    *
    * @SWG\SecurityScheme(
    *     securityDefinition="Bearer",
    *     type="apiKey",
    *     in="header",
    *     name="Authorization"
    * )
    *
    */

2、在需要认证的接口添加如下代码

   /**
    *
    * @SWG\Get(
    *     path="api/user/info",
    *     tags={"User"},
    *     summary="User Info",
    *     description="Get User Info",
    *     security={
    *          {
    *              "Bearer":{}
    *          }
    *      },
    *     @SWG\Response(
    *          response="200",
    *          description="请求成功",
    *     )
    * )
    *
    */

3、创建中间件

class SwaggerFix
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (strpos($request->headers->get("Authorization"),"Bearer ") === false) {
            $request->headers->set("Authorization","Bearer ".$request->headers->get("Authorization"));
        }
        return $next($request);
    }
}

4、在Kernel.php添加中间件

protected $routeMiddleware = [
    //最好放在第一个位置
    'swfix' => \App\Http\Middleware\SwaggerFix::class,
]

5、在路由文件中设置此中间件

$api->group([ 'middleware' => ['api.auth','swfix'] ], function ($api) {

}

6、生成API文档
用的laravel框架,只需执行php artisan l5-swagger:generate方法即可

7、打开本地配置的Api文档地址,如:127.0.0.1/api/documentation
token

auth

传送门

Bearer Authentication
oauth2 + passport = Bearer
Authorization:Bearer

posted @ 2018-05-18 11:39  littleBit  阅读(5776)  评论(0编辑  收藏  举报