340.3. API ドキュメントにセキュリティー定義を追加する

Camel 2.22.0 以降で利用可能

Rest DSL は、生成された API ドキュメントでの Swagger 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")

ここでは、2 つのセキュリティー定義を設定しています。

  • OAuth2 - 提供された URL による暗黙の承認
  • API キー - 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")

ここで、get 操作は Api Key セキュリティーを使用しており、put 操作は読み取りおよび書き込み PET の許可範囲で OAuth セキュリティーを使用しています。