我正在使用带有Flask-connexion的swagger 2.0 API。
在swagger.yml文件中,我将安全性定义设置为基本:
BasicAuth:
type: basic
然后,我将此安全性添加到要保护的路径中。
# Tenant paths
/tenant:
get:
operationId: tenant.read_all
tags:
- Tenant
summary: Read the entire set of tenants, sorted by name
description: Read the entire set of tenants, sorted by name
security:
- basicAuth: []
responses:
200:
description: Successfully read tenant set operation
schema:
type: array
items:
$ref: '#/definitions/Tenant'
但是我不明白如何指定将验证登录名和密码的功能。我需要收集这些参数并在调用路径函数之前验证它们。
例如,如果使用Flask-Login或Flask-BasicAuth进行隐式定义?
或者应该通过在我的tenant.py文件中添加代码来像没有Flask-connexion那样显式地完成此操作:
@auth_basic.login_required
def read_all():
...
我希望将Flask-connexion重定向到auth函数,该函数将验证登录名和密码,然后重定向到path方法/函数。
关于雷米
https://connexion.readthedocs.io/en/latest/security.html#basic-authentication
您必须在Swagger文件中定义:
securityDefinitions:
basic:
type: basic
x-basicInfoFunc: app.basic_auth
x-basicInfoFunc将映射到验证函数,在此示例中,函数basic_auth位于app文件中。
使用Swagger的完整示例:https://github.com/zalando/connexion/tree/master/examples/swagger2/basicauth
OpenApi的完整示例:https://github.com/zalando/connexion/tree/master/examples/openapi3/basicauth