78.3. API ドキュメントにセキュリティー定義を追加する
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")
ここでは、2 つのセキュリティー定義を設定しました。
- OAuth2 - 提供された URL で暗黙の認証を行います。
- Api Key - myHeader という HTTP ヘッダーに由来する Api Key を使用します。
そして、残りの操作で、どのセキュリティーを使用するか、そのキー (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 操作には OAuth のセキュリティーが、許可されたスコープとして read と write のペットが使用されています。