Why is the Authorization header missing in requests sent from Swagger UI?

橙三吉。 提交于 2020-07-31 04:50:41

问题


I want to add a documentation to my Node.js API, for this I have a YAML file where I put my definitions, the swagger doc is at localhost:5000/api-doc and working fine.

Now I have to add Bearer authorization but Swagger with the following definition:

swagger: "2.0"
info:
    version: 1.0.0
    title: My API documentation
    description: >
        My API documentation

host: localhost:5000
basePath: "/v1"
schemes:
    - http
securityDefinitions:
    Bearer:
        type: apiKey
        description: "Value: Bearer "
        name: Authorization
        in: header
paths:
    /users:
        get:
            responses:
                "200":
                    description: "Will send `Authenticated`"
                "403":
                    description: "You do not have necessary permissions for the resource"

When testing the request (I clicked on "Authorize" button at the top right and entered my token) I get following error:

"error": "Authorization header not found.

Why is the Authorization header not included in the request?


回答1:


securityDefinitions alone aren't enough, you also need to add the security key on the root level or operation level to actually apply the security.

security:
  - Bearer: []


来源:https://stackoverflow.com/questions/61477056/why-is-the-authorization-header-missing-in-requests-sent-from-swagger-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!