2017-04-05 206 views
0

我正在使用Swagger 2.0和Swagger UI 3.0.3。Swagger UI不向请求添加标头

在我api_definition.yaml我有我的路径前执行以下操作:

swagger: '2.0' 

################################################################################ 
#     Host, Base Path, Schemes and Content Types     # 
################################################################################ 
# Metadata 
info: 
    version: v1 
    title: Microservice 
    description: Microservice API! 


host: sandbox 

basePath: '/apps/fiji/v1' 
schemes: 
    - http 

securityDefinitions: 
    apikey: 
    type: apiKey 
    name: X-Access-Token 
    in: header 

security: 
    - apikey: [] 

produces: 
    - application/json 
consumes: 
    - application/json 

这增加了授权按钮招摇UI,用户可以在他们的API密钥粘贴。我希望将这个API密钥发送到每个请求的请求头中。这虽然没有发生,我不知道为什么。我错过了什么吗?

编辑:

请求似乎并送我回去401 Unauthorized

Chrome开发工具显示了以下请求标头:

GET /apps/fiji/v1/getCPICountries HTTP/1.1 
Host: sandbox 
Connection: keep-alive 
accept: application/json 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 
content-type: application/json 
Referer: http://sandbox/apps/fiji/vendor/swagger-ui/dist/index.html?url=http://sandbox/apps/fiji/swagger/api_definition.yaml 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 

我必须设置为路径:

# API Paths 
paths: 
    # getCPICountries endpoint 
    /getCPICountries: 
    # HTTP operations 
    get: 
     # Describe this verb here. Note: you can use markdown 
     description: | 
     Returns a list of countries and country codes 
     produces: 
     - application/json 
     security: 
     - auth: 
      - role_admin 
     # Expected responses for this operation: 
     responses: 
     # Response code 
     200: 
      description: Successful response 
      # A schema describing your response object. 
      # Use JSON Schema format 
      schema: 
      properties: 
       data: 
       type: array 
       items: 
        $ref: '#/definitions/CPIResponse' 

和定义如下:

definitions: 
    CPIResponse: 
    type: object 
    UserObject: 
    type: object 
    properties: 
     email: 
     type: string 
     id: 
     type: number 
     orgId: 
     type: number 
     firstName: 
     type: string 
     lastName: 
     type: string 
+0

您的规格是有效的,类似的配置适用于我最新的主人。你可以发布有问题的操作规范吗?请求是否实际发送,例如如果您检查浏览器开发工具中的网络选项卡? – Helen

+0

@ Helen更新请求头。我肯定发送了请求,然后回来:'{“message”:“令牌在TokenStorage中找不到。”} –

+0

您可以发布操作定义(YAML)吗? – Helen

回答

1

的问题是我在我的路径中覆盖了security。我需要删除以下内容:

security: 
    - auth: 
    - role_admin