gpt4 book ai didi

swagger-ui - 如何在swagger ui v.3.0中使用基本身份验证

转载 作者:行者123 更新时间:2023-12-04 12:17:21 26 4
gpt4 key购买 nike

在swagger ui 2.0中,它是代码

var basicAuth = new SwaggerClient.PasswordAuthorization("basicAuth", username, password);
window.swaggerUi.api.clientAuthorizations.add("basicAuth", basicAuth);

有人可以为swagger ui 3.0提供代码吗?

谢谢。

编辑。
我正在尝试做这样的事情- Adding Basic Authorization for Swagger-UI

我在具有基本身份验证的服务器上使用Swagger。所以我不能初始化库。
  const ui = SwaggerUIBundle({
url: "http://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
// yay ES6 modules ↘
Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui

没有基本身份验证,一切正常。

启用基本身份验证- http://prntscr.com/enxee4

最佳答案

在Swagger UI 3.x中,在版本3中支持获取受基本auth/API key 保护的规范(.yaml/.json文件)。 3.3.2及更高版本。在您的Swagger UI初始化代码中,定义一个requestinterceptor,它将auth头附加到spec fetch请求:

<!-- index.html -->

const ui = SwaggerUIBundle({
url: "http://petstore.swagger.io/v2/swagger.json",

requestInterceptor: (req) => {
if (req.loadSpec) {
// Fetch the spec using Basic auth, replace "user" and "password" with yours
req.headers.Authorization = 'Basic ' + btoa('user:password');

// or API key
// req.headers.MyApiKey = 'abcde12345';

// or bearer token
// req.headers.Authorization = 'Bearer abcde12345';
}
return req;
},
...
})

关于swagger-ui - 如何在swagger ui v.3.0中使用基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com