简体   繁体   English

如何获得 Swagger UI 让我提供身份验证 header?

[英]How do I get Swagger UI to let me provide authentication header?

I used Luminus, along with reitit and swagger-ui to generate a page that lets me try out my Luminus API.我使用 Luminus,连同 reitit 和 swagger-ui 来生成一个页面,让我试用我的 Luminus API。 I can just enter my API request body and submit to test my API.我可以输入我的 API 请求正文并提交以测试我的 API。 我的招摇 UI 的快照 Now I have added authentication using buddy and my API requires a token to be passed in the header of the request, or else it rejects the request as forbidden.现在我已经使用伙伴添加了身份验证,并且我的 API 需要在请求的 header 中传递一个令牌,否则它会拒绝该请求。
I'm trying to get an "Authorization" headers field to magically appear in my UI so that I can enter a JWT token string and test my API.我试图让“授权”标题字段神奇地出现在我的 UI 中,以便我可以输入 JWT 令牌字符串并测试我的 API。 This must be a very common requirement for anyone creating an API using reitit, but I can't figure out how to do it.对于使用 reitit 创建 API 的任何人来说,这一定是一个非常普遍的要求,但我不知道该怎么做。 I went searching around and found this reitit issues page , which includes the text...我四处寻找,发现了这个 reitit 问题页面,其中包括文本......

Header params are declared as lower-case strings {:headers {"authorization" string?}} which would match exactly what Ring provides us. Header 参数被声明为小写字符串 {:headers {"authorization" string?}} 这将与 Ring 提供给我们的完全匹配。 (Could still HTTP-Header-Case them for documentation.) (仍然可以使用 HTTP-Header-Case 获取文档。)

...and suggests the setup below... ...并建议以下设置...

:get {:summary "list offers"
      :parameters 
        {:headers 
          {"authorization" string?}}
         ... etc

Doing that didn't get me any way to authenticate.这样做并没有让我通过任何方式进行身份验证。 So, I found this discussion and edited my routes by adding the following after:summary and:parameters in the above route...所以,我找到了这个讨论并通过在上述路线中添加以下内容来编辑我的路线:summary 和:parameters...

             :middleware [authenticated?]
             :swagger {:security [:apiKey]}

After adding that, I get this...添加之后,我得到了这个...... 在此处输入图像描述

...which looks like I'm on the right track, but I still have no way of entering my token in the auth header. ...看起来我在正确的轨道上,但我仍然无法在身份验证 header 中输入我的令牌。

As I said, everything is working with curl... Just that swagger isn't showing any way to add auth header.正如我所说,一切都与 curl 一起工作......只是 swagger 没有显示任何添加身份验证 header 的方法。 Anybody know how to get Swagger UI to play along in this scenario?有人知道如何让 Swagger UI 在这种情况下一起玩吗?

If reitit doesn't support this, then how are people using Swagger UI for authenticated requests?如果 reitit 不支持这一点,那么人们如何使用 Swagger UI 来处理经过身份验证的请求?

Any help would be appreciated!任何帮助,将不胜感激!

OK.好的。 Solved it.解决了。

At the root of my API routes (encapsulating all of the routes where I might end up using this authentication), I add:securityDefinitions.在我的 API 路由的根目录(封装了我可能最终使用此身份验证的所有路由),我添加:securityDefinitions。

  ["/api"
   {:swagger {:id ::api
              :securityDefinitions {:apiAuth
                                             {:type "apiKey"
                                              :name "Authorization"
                                              :in "header"
                                              }}}

Inside the specific route:具体路线内:

             :middleware [authenticated?]
             :swagger {:security [{:apiAuth []}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM