【问题标题】:swagger oauth got it, so now what?swagger oauth 得到了它,那么现在呢?
【发布时间】:2018-05-08 14:18:14
【问题描述】:

我想将 oauth2 身份验证添加到我的 Web API,因此我创建了一个带有安全定义的规范:

"securityDefinitions": {
    "OauthSecurity": {
        "type": "oauth2",
        "authorizationUrl": "http://localhost/oauth/dialog", 
        "flow": "implicit",
            "scopes": {
                "admin": "Admin scope",
                "user": "User scope",
                "media": "Media scope"
            }
    }
},
"security": [
    {
        "OauthSecurity": [
            "user"
        ]
    }
],

所以它在我的 API 上生成了一些注释:

@io.swagger.annotations.Authorization(value = "OauthSecurity", scopes = {
        @io.swagger.annotations.AuthorizationScope(scope = "user", description = "User scope")
    })

那么我现在该如何继续呢?我仍然可以毫无问题地访问(没有任何标题或令牌或其他任何东西的卷曲)我的 API。

当然我可以创建 auth endpdoint,但我不知何故错过了生成的 API 和 oauth 之间的链接。

【问题讨论】:

  • 除非您在 API 端指定用户或媒体或管理员范围指定的内容,否则我怀疑这会起作用。
  • 整个 api 现在是这个设置的用户范围。当我从 swagger 中查看 petstore 示例时,他们使用了 read_pets 和 write_pets 但也没有明确指定它们 editor.swagger.io/#
  • 这有什么更新吗?我也找不到关于该主题的好的文档。
  • @SuleymanArslan 看到我的答案

标签: java rest authentication oauth-2.0 swagger


【解决方案1】:

有一段时间了,根据this,通过使用 OpenAPI 3.0 你可以做到:

components:
  securitySchemes:
    oAuthSample:    # <---- arbitrary name
      type: oauth2
      description: This API uses OAuth 2 with the implicit grant flow. [More info](https://api.example.com/docs/auth)
      flows:
        implicit:   # <---- OAuth flow(authorizationCode, implicit, password or clientCredentials)
          authorizationUrl: https://api.example.com/oauth2/authorize
          scopes:
            read_pets: read your pets
            write_pets: modify pets in your account

我目前正在使用Bearer Auth,效果很好。

希望对你有帮助!

【讨论】:

猜你喜欢
  • 2021-01-27
  • 2019-12-25
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2015-02-26
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多