【发布时间】:2018-06-26 18:40:33
【问题描述】:
早安,
我有一个带有 FOSRestBundle 和 NelmioApiDocBundle 的 Symfony API 项目。 我不知道如何使用 FOS Rest 添加安全注释。 我使用的是 OAuth v2,所以我的安全性基于以下几点:
apiKey: accessToken, refreshToken.
这是我在 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[]
*/
在这种情况下,我有一个例外:
不允许在“ApiBundle\Controller\Api\V1\UserRentAdController::listAction()”中使用注解“Swagger\Annotations\SecurityScheme”作为根注解。
请帮帮我。
【问题讨论】:
标签: symfony annotations swagger fosrestbundle nelmioapidocbundle