swagger发请求的时候如何自动添加Bearer

private List<ApiKey> securitySchemes() {
    List<ApiKey> apiKeyList= new ArrayList();
    apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
    return apiKeyList;
}

生成的添加Token的对话框如下:
图片描述

用户输入“Bearer ${token}”,这样比较麻烦,怎样实现用户只需要输入${token},swagger能够自动添加上“Bearer”

阅读 7k
1 个回答

请使用 Bearer Authentication 而不是 apiKey 的方式。
它将会在 http header 中自动增加

Authorization: Bearer <token>

schema 配置

openapi: 3.0.0
...
# 1) Define the security scheme type (HTTP bearer)
components:
  securitySchemes:
    bearerAuth:            # arbitrary name for the security scheme
      type: http
      scheme: bearer
      bearerFormat: JWT    # optional, arbitrary value for documentation purposes
# 2) Apply the security globally to all operations
security:
  - bearerAuth: []         # use the same name as above

Bearer Authentication

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题