1

再会,

我有一个带有FOSRestBundleNelmioApiDocBundle的 Symfony API 项目。我不知道如何使用 FOS Rest 添加安全注释。我正在使用 OAuth v2,因此我的安全性基于以下几点:

apiKey:访问令牌、刷新令牌

这是我在app/config中的 nelmio api 包配置:

nelmio_api_doc:
    areas:
        path_patterns: # an array of regexps
            - ^/api/v1(?!/doc$)
    documentation:
        info:
            title: Ads api documentation
            description: Swagger api documentation
            version: 1.0.0
        securityDefinitions:
            api_key:
                type: apiKey
                description: "Your Json Web Token, dont forget to preprend 'Bearer'"
                name: Authorization
                in: header
        security:
            api_key: []

我的一个控制器中的一个路由示例:

/**
 * @Rest\View(statusCode=200, serializerGroups={"rentAdList", "time"})
 * @Rest\Get("", name="api_v1_user_ad_list")
 *
 * @SWG\Tag(name="user_ad")
 * @SWG\Response(
 *     response=200,
 *     description="Display ad",
 *     @SWG\Schema(
 *      @Model(type=AdBundle\Entity\RentAd::class, groups={"rentAdList", "time"})
 *     )
 * )
 *
 * @return RentAd[]
 */
public function listAction()
{
    $user           = $this->get('security.token_storage')->getToken()->getUser();
    $rentAdDataProvider = $this->get('ad.data_provider.rent_ad_data_provider');
    $rentAds        = $rentAdDataProvider->getRentAdsByUser($user);

    return $rentAds;
}

所以我的问题是如何将 Swagger 安全注释与 api_key 一起使用,角色为USER_ROLE

我试图添加:

/**
 * @Rest\View(statusCode=200, serializerGroups={"rentAdList", "time"})
 * @Rest\Get("", name="api_v1_user_ad_list")
 *
 * @SWG\Tag(name="user_ad")
 * @SWG\Response(
 *     response=200,
 *     description="Display ad",
 *     @SWG\Schema(
 *      @Model(type=AdBundle\Entity\RentAd::class, groups={"rentAdList", "time"})
 *     )
 * )
 * @SWG\SecurityScheme(name="apiKey")
 *
 * @return RentAd[]
 */

在这种情况下,我有一个例外:

不允许使用注释“Swagger\Annotations\SecurityScheme”作为“ApiBundle\Controller\Api\V1\UserRentAdController::listAction()”中的根注释。

请帮我。

4

1 回答 1

-1

有那么一刻,我找到了唯一适合我的解决方案。
我在Controller中添加了两个注释

* @SWG\Parameter(name="Authorization", in="header", required=true, type="string", default="Bearer accessToken", description="Authorization")
* @Security(name="Bearer")`

在这种情况下,在 api cods 中,您将拥有附加参数Authorisation : Bearer {accessToken}

于 2018-06-27T08:21:29.993 回答