【问题标题】:Use HTTPS with Basic Auth in Swagger securitySchemes在 Swagger securitySchemes 中将 HTTPS 与基本身份验证结合使用
【发布时间】:2023-02-17 16:55:08
【问题描述】:
我有这个应用程序,我在其中使用 Swagger 来显示它的端点。我使用 Basic Auth,出于安全考虑,我希望 Swagger UI 在“试用”中使用 HTTPS 发出 HTTP 请求。
在 Swagger documentation 它说
该方案必须具有类型:http
但是,这是否意味着无法使用 Basic Auth 发出安全请求?我知道身份验证方法本身是“弱的”,但我读到它在使用 HTTPS 时表现得像表单 POST。
【问题讨论】:
标签:
https
swagger
swagger-ui
openapi
basic-authentication
【解决方案1】:
您似乎混淆了一些 OpenAPI 关键字和 HTTP 概念。
type: http在securitySchemes指的是身份验证方法,在本例中为 HTTP authentication,它指的是使用 Authorization HTTP 请求标头来发送凭据。基本身份验证是 HTTP 身份验证的一种实现方式。
请求 URL 的 http:// 或 https:// 部分是 protocol。在 OpenAPI 3 中,该协议被定义为 servers URL 的一部分。在 OpenAPI 2.0 中,它是使用 schemes 关键字定义的。详情请见:
例子
在 OpenAPI 3.0 中,可以按如下方式定义使用基本身份验证通过 HTTPS 发送到 https://httpbin.org/basic-auth/user/passwd 的请求:
openapi: 3.0.0
info:
title: HTTPS + Basic auth example
version: 1.0.0
servers:
- url: https://httpbin.org
components:
securitySchemes:
basicAuth:
type: http
scheme: basic
description: Use `user` / `passwd` as the test credentials
paths:
/basic-auth/user/passwd:
get:
security:
- basicAuth: []
responses:
'200':
description: OK
'401':
description: Unauthorized. The username or password provided is incorrect.
【解决方案2】:
我在我的 (tomcat 9 Jax-rs jersey) 中部署了一个网络服务 我在使用 http 连接时配置了一个 swagger 测试 http://localhost:8080/myws 它工作正常
但在 https://remoteserver/myws 的远程服务器中部署了相同的 ws 我得到了
获取失败。
可能的原因:
CORS
网络故障
对于 CORS 请求,URL 方案必须是“http”或“https”。
当我尝试使用 curl 命令行时,它工作正常
你有什么想法吗