如何在 swagger django 中添加 Bearer {JWT}?

How to add Bearer {JWT} in swagger django?

我在Swagger中给自己授权的时候UI,我这里要写“Bearer {then I write JWT}”

如何在 swagger UI 中的 JWT 令牌前自动添加字符串“Bearer”? 这是我的 Swagger 设置:

SWAGGER_SETTINGS = {
    "SECURITY_DEFINITIONS": {
        "JWT [Bearer {JWT}]": {
            "name": "Authorization",
            "type": "apiKey",
            "in": "header",
        }
    },
    "USE_SESSION_AUTH": False,
}

FORCE_SCRIPT_NAME = "/"

我建议您从 drf-yasg 迁移到 drf_spectacular, it already includes JWT authentication automatically and without so many complications, it even uses OpenAPI 3drf-yasg 使用 OpenAPI 2 并且即将过时)

自动生成

使用 JWT 进行身份验证

看到这个From drf-yasg to OpenAPI 3