Swagger Authorization:bearer


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM