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

查看:619
本文介绍了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

@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实现之前由过滤器或其他资源使用的参数.
  • Using Servlets which don't use JAX-RS annotations.
  • Wanting to hide a parameter as it is defined and override it with a completely different definition.
  • Describe a parameter that is used by a filter or another resource prior to reaching the JAX-RS implementation.

Swagger用户界面将更新,因此您可以从那里发送令牌.无需更改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时,我意识到只需添加 @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天全站免登陆