Swagger UI 将身份验证令牌传递给标头中的 API 调用 [英] Swagger UI passing authentication token to API call in header

查看:31
本文介绍了Swagger UI 将身份验证令牌传递给标头中的 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swagger 的新手.

I am new to Swagger.

我正在使用 Swagger UI 生成 swagger 文档.我有两个 API 调用.第一个调用是根据用户名和密码生成令牌.第二次调用需要第一次调用生成的令牌.

I am using Swagger UI to generate swagger documentation. I have two API calls. First call is to generate token based on user name and password. Second call needs token generated by first call.

我如何使用 Swagger UI 为第二次调用设置该令牌?

How I set that token for second call using Swagger UI?

推荐答案

@ApiImplicitParams@ApiImplicitParam 应该可以解决问题:

@ApiImplicitParams and @ApiImplicitParam should do the trick:

@GET
@Produces("application/json")
@ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "Authorization token", 
                      required = true, dataType = "string", paramType = "header") })
public String getUser(@PathParam("username") String userName) {
    ...
}

来自文档:

您可能希望手动描述操作参数.这可能有多种原因,例如:

You may wish you describe operation parameters manually. This can be for various reasons, for example:

  • 使用不使用 JAX-RS 注释的 Servlet.
  • 想要隐藏定义的参数并使用完全不同的定义覆盖它.
  • 描述过滤器或其他资源在到达 JAX-RS 实现之前使用的参数.

Swagger UI 将更新,以便您可以从那里发送您的令牌.无需更改 HTML.

The Swagger UI will be updated so you can send your token from there. No changes to HTML will be necessary.

注意:不久前,在使用 Swagger 记录 REST API 时,我意识到只需添加 @ApiImplicitParam 是不够的(即使你只有一个参数).无论如何,您必须添加 @ApiImplicitParams 也是.

Note: A while ago, when documenting a REST API with Swagger, I realized that just adding @ApiImplicitParam is not enough (even if you have only one parameter). Anyway, you must add @ApiImplicitParams too.

这篇关于Swagger UI 将身份验证令牌传递给标头中的 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆