使用 Swagger 3.0 发送 cookie 会话 ID [英] Sending cookie session id with Swagger 3.0

查看:76
本文介绍了使用 Swagger 3.0 发送 cookie 会话 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说要定义 cookie 身份验证,请改用 API 密钥."在官方文档中

It is said that "To define cookie authentication, use API keys instead." in the official documentation

https://swagger.io/docs/specification/describing-parameters/#cookie-参数

事实是我们尝试过

components:
  securitySchemes:
    cookieAuth:         
      type: apiKey
      in: cookie
      name: sessionId
...
    security:
      - cookieAuth: []

使用上面的代码,在 Swagger UI 中,我们可以点击挂锁来设置 sessionId 的值.但是当我们执行该方法时,cookie的值为NULL,我们在Headers(Chrome开发者工具)中没有看到发送的cookie

Using the above code, in Swagger UI we are able to click on the padlock to set the value of the sessionId. But when we execute the method, the value of the cookie is NULL and we don't see the cookie sent in the Headers (Chrome Developer tool)

我也尝试将其放入 cookie 参数中,如下所示:

I tried also to put that in cookie parameter like this:

parameters:
  - in: cookie
    name: sessionId   
    required: true
    schema:
      type: string 

但是,同样的结果(到达空值,在调试器工具中什么也没有.

But there again, same result (arrive null, and nothing in the debugger tool.

我们使用 Swagger 和 openApi 3.0,其他参数,requestBody 运行良好,但不是这个 cookie 传输.

We use Swagger with openApi 3.0, other parameters, requestBody is working well, but not this cookie transmission.

感谢任何有想法的人.

推荐答案

Swagger UI 和 Swagger Editor 目前不支持在试用"请求中发送 cookie:
https://github.com/swagger-api/swagger-js/issues/1163

Swagger UI and Swagger Editor currently do not support sending cookies in "try it out" requests:
https://github.com/swagger-api/swagger-js/issues/1163

正如开发人员所解释的那样,问题在于几乎不可能从浏览器中将任意 cookie 数据发送到不同的来源.

As the developers explain, the issue is that it's almost impossible to send arbitrary cookie data to a different origin from within the browser.


SwaggerHub 支持 cookie auth 和 cookie 参数.SwaggerHub 从其服务器而不是从您的浏览器发送试用"请求,这使得发送带有用户定义的 Cookie 标头的请求成为可能.


SwaggerHub supports cookie auth and cookie parameters though. SwaggerHub sends "try it out" requests from its servers rather than from your browser, which makes it possible to send requests with a user-defined Cookie header.

披露:我为制作 SwaggerHub 的公司工作.

这篇关于使用 Swagger 3.0 发送 cookie 会话 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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