在招摇的Laravel中使用JWT Bearer令牌

Alf*_*ani 2 jwt laravel swagger

我在我的项目中使用DarkaOnLine的L5-swagger。我想在文档中使用JWT Auth。我添加了以下代码:

/**
  @OAS\SecurityScheme(
      securityScheme="API Key Auth",
      type="apiKey",
      in="header",
      name="Authorization",
  )
 **/
Run Code Online (Sandbox Code Playgroud)

在庄重的用户界面中,显示了“授权”按钮,并且有一个表单可以填充令牌。但是在输入它之后,我仍然在需要令牌访问的函数中收到“ token_not_provided”错误。

提前致谢。

小智 6

您可以将 swagger 配置修改为如下所示。即配置/l5-swagger.php

'bearer_token' => [ // Unique name of security
   'type' => 'apiKey', // Valid values are "basic", "apiKey" or "oauth2".
   'description' => 'Enter token in format (Bearer <token>)',
   'name' => 'Authorization', // The name of the header or query parameter to be used.
   'in' => 'header', // The location of the API key. Valid values are "query" or "header".
],
Run Code Online (Sandbox Code Playgroud)

然后添加修改您的操作,使其看起来像这样。

    /**
     * @OA\Get(
     * ...
     *     security={{"bearer_token":{}}}
     * ...
     * )
     */
Run Code Online (Sandbox Code Playgroud)

请注意,bearer_token此处使用的与证券的唯一名称相同。使用这种方法,您无需对中间件进行更改。


Hel*_*len 5

@OAS注释用于OpenAPI 3.0,其中承载身份验证定义为type: http+ scheme: bearer

/**
  @OAS\SecurityScheme(
      securityScheme="bearerAuth",
      type="http",
      scheme="bearer"
  )
 **/
Run Code Online (Sandbox Code Playgroud)

确保您的操作使用securitysecurityScheme="<NAME>"上面指定的名称相同的名称。例如:

/**
 * @OAS\Get(
 *   ...
 *   security={{"bearerAuth":{}}}
 *   ...
Run Code Online (Sandbox Code Playgroud)

在Swagger UI的“授权”对话框中,输入不带“ Bearer”前缀的令牌。


Em.*_*.MF 5

这个链接解决了我的问题。 承载授权设置

我将它用于Laravel Lumen、JWT身份验证。


将此添加到您的中间件或任何其他地方,如基本控制器

/**
 * @OA\SecurityScheme(
 *     type="http",
 *     description="Login with email and password to get the authentication token",
 *     name="Token based Based",
 *     in="header",
 *     scheme="bearer",
 *     bearerFormat="JWT",
 *     securityScheme="apiAuth",
 * )
 */
Run Code Online (Sandbox Code Playgroud)

并将其添加到您的操作/功能中。

/**
 * @OA\Get(
 *  path="/resources",
 *  summary="Get the list of resources",
 *  tags={"Resource"},
 *  @OA\Response(response=200, description="Return a list of resources"),
 *  security={{ "apiAuth": {} }}
 * )
 */
Run Code Online (Sandbox Code Playgroud)