2

I am using swaggo (https://github.com/swaggo/swag) to auto-create a working swagger specification for my API.

The swagger spec allows me to run all of my API endpoints and receive responses.

I then added JWT authentication to all of my endpoints. Now I'm unable to use the swagger spec to run any endpoints, as it always fails auth.

What annotations do I need to add to each endpoint, that will configure the Swagger spec to allow a JWT to be passed?

I've read the README at https://github.com/swaggo/swag and Google searched, to no avail.

4

4 回答 4

5

您将在 main.go 中需要这些(这会将格式的令牌发送到您的标题:“ YourTokenNametoken”):

//@securityDefinitions.apikey <YourTypeOfKey>
//@in header
//@name <YourTokenName>

然后将此注释添加到需要身份验证令牌的每个端点:

// @Security <YourTypeOfKey>
于 2020-09-14T10:05:43.277 回答
4

似乎添加到每个端点的这些评论起到了作用......

// @Security ApiKeyAuth
// @param Authorization header string true "Authorization"

这条评论也被添加到我们的 main.go 文件中

// @securityDefinitions.apikey ApiKeyAuth
于 2019-05-20T22:20:36.427 回答
0

// @Param Authorization header string true "Insert your access token" default(Bearer <Add access token here>)

将此参数添加到 swagger v2.0 中每个端点的标头

在 V3.0 中,我们可以直接访问不记名令牌。

于 2021-01-11T10:00:52.930 回答
0

看起来您添加的 JWT 中间件将在 swaaggo/swag 的处理程序之前被调用。你能发布你的路由设置吗?

于 2019-05-21T02:40:37.720 回答