如何获取Swagger UI以提供身份验证标头?

时间:2020-04-27 21:58:10

标签: authentication clojure swagger-ui luminus reitit

我使用Luminus以及reitit和swagger-ui生成了一个页面,可以让我试用Luminus API。我可以输入我的API请求正文并提交以测试我的API。 A snapshot of my swagger UI 现在,我使用伙伴添加了身份验证,并且我的API需要在请求的标头中传递令牌,否则它将被拒绝,因为请求被禁止。
我正在尝试获取一个“授权”标头字段,以使其神奇地出现在我的UI中,以便我可以输入JWT令牌字符串并测试我的API。对于使用reitt创建API的任何人来说,这必须是一个非常普遍的要求,但是我不知道该怎么做。 我到处搜寻,发现this reitit issues page,其中包含文字...

标头参数声明为小写的字符串{:headers {“ authorization” string?}},它与Ring向我们提供的内容完全匹配。 (仍然可以使用HTTP-Header-Case来获取文档。)

...并建议以下设置...

library(ISLR)

data <- Auto

# Get Data Types of all Columns in the Data Frame
column_types <- sapply(data, class)

# Create a vector to hold all the unwanted data types (Change to what you want)
unwanted_types <- c("factor", "character")

# Remove all the columns which are of the unwanted datatypes
data <- data[, which(!column_types %in% unwanted_types)]

# Get Summary of the DataFrame
summary(data)

mpg          cylinders      displacement     horsepower        weight      acceleration        year           origin     
 Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613   Min.   : 8.00   Min.   :70.00   Min.   :1.000  
 1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225   1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000  
 Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804   Median :15.50   Median :76.00   Median :1.000  
 Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978   Mean   :15.54   Mean   :75.98   Mean   :1.577  
 3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615   3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000  
 Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140   Max.   :24.80   Max.   :82.00   Max.   :3.000 

这样做并没有使我进行身份验证的任何方式。因此,我找到了this discussion并通过在上述路线的:summary和:parameters之后添加以下内容来编辑路线...

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

添加完之后,我得到了... enter image description here

...看起来好像我在正确的轨道上,但是我仍然无法在auth标头中输入我的令牌。

正如我所说的,一切都在使用curl ...只是那张招摇没有显示任何添加auth标头的方法。有人知道如何在这种情况下使用Swagger UI吗?

如果reitit不支持此功能,那么人们如何使用Swagger UI进行经过身份验证的请求?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

好。解决了。​​

在我的API路由的根(封装所有可能最终使用此身份验证的路由)中,添加:securityDefinitions。

[2020-04-28 11:15:55,904] ERROR in app: Exception on /system [GET]
Traceback (most recent call last):
  File "./web_base.py", line 35, in system
    temp_humidity = round(sense.get_temperature_from_humidity(),1)
  File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 582, in get_temperature_from_humidity
    self._init_humidity()  # Ensure humidity sensor is initialised
  File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 549, in _init_humidity
    raise OSError('Humidity Init Failed')
OSError: Humidity Init Failed

在特定路线内:

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