Swagger 和 JWT 令牌认证 [英] Swagger and JWT Token Authentication

查看:46
本文介绍了Swagger 和 JWT 令牌认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一些 Swagger 文档,一切都很好,除了我想让页面以交互方式工作,所以在选择编辑器或 UI 时,如果我点击授权按钮,我会调用我的身份验证 URL 来构建然后在后续请求中使用的 JWT 令牌.

I am building some Swagger documentation, all well and good, except that I am wanting to have the page work interactively, so when selecting the editor or UI, if I hit the authorize button, I would call my Authentication URL that builds the JWT token that is then used in subsequent requests.

我计划向 API 客户端颁发一个 Api 访问密钥和一个秘密访问密钥,并希望访问一个身份验证页面,该页面将处理这些并构建 JWT 令牌.

I am planning to issue the API client an Api Access Key and a Secret Access Key, and want to hit an authentication page that will process these and build the JWT token.

令我震惊的是,如果我能在 Swagger 中正确定义如何实现这一目标,我将拥有一个现成的测试客户端,然后可以针对我的新代码使用.

It strikes me that if I can get the correct definition of how to achieve this in Swagger, that I will have a ready-built test client to then use against my fresh new code.

是的,这是我第一次使用 JWT,我还没有构建代码.你能说API-First"吗?

Yes, it's my first time with JWT and I have not yet built the code. Can you say "API-First"?

推荐答案

这就是我使用 Swagger 和 JWT 身份验证的方式:

This is how I used Swagger with JWT Authentication:

  • 编写 Express.js API 端点以生成 JWT.
  • 创建一个 Swagger 路径以使用上述端点检索 JWT
  • 在 swagger.yaml 根级别:

  • Write a Express.js API end point to generate a JWT.
  • Create a Swagger Path to retrieve the JWT using above end point
  • In swagger.yaml root level:

securityDefinitions:  
  JWT:  
    type: apiKey  
    in: header  
    name: access_token  

  • 在 swagger.yaml 路径中:

  • In swagger.yaml paths:

    security  
     -JWT: []
    

  • 这将在浏览器的 Swagger UI 中显示一个授权按钮.

    This will display an Authorize button in Swagger UI on browser.

    • 在点击上面的Authorize按钮时弹出的Authentication Window中输入上面生成的JWT
    • 现在 JWT 将与请求标头一起传递

    希望这对其他人有帮助.

    Hope this may help others.

    这篇关于Swagger 和 JWT 令牌认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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