第 334 章 在 API 中添加安全定义


可从 Camel 3.1.0 开始

Rest DSL 现在支持在生成的 API 文档中声明 OpenApi securityDefinitions。例如,如下所示:

rest("/user").tag("dude").description("User rest service")
    // setup security definitions
    .securityDefinitions()
        .oauth2("petstore_auth").authorizationUrl("http://petstore.swagger.io/oauth/dialog").end()
        .apiKey("api_key").withHeader("myHeader").end()
    .end()
    .consumes("application/json").produces("application/json")
Copy to Clipboard Toggle word wrap

此处已设置两个安全定义

  • OAuth2 - 带有提供的 url 的隐式授权
  • API Key - 使用来自名为 myHeader的 HTTP 标头的 api 键

然后,您需要通过引用密钥(petstore_auth 或 api_key)来指定安全使用的剩余操作。

.get("/{id}/{date}").description("Find user by id and date").outType(User.class)
    .security("api_key")

...

.put().description("Updates or create a user").type(User.class)
    .security("petstore_auth", "write:pets,read:pets")
Copy to Clipboard Toggle word wrap

此处的 get 操作使用 Api Key 安全性,而 放置操作则正在使用 OAuth 安全性,具有允许的读取和写入宠物。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat