Chapter 337. Adding Security Definitions in API doc

Available as of Camel 3.1.0

The Rest DSL now supports declaring OpenApi securityDefinitions in the generated API document. For example as shown below:

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")

Here we have setup two security definitions

  • OAuth2 - with implicit authorization with the provided url
  • Api Key - using an api key that comes from HTTP header named myHeader

Then you need to specify on the rest operations which security to use by referring to their key (petstore_auth or 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")

Here the get operation is using the Api Key security and the put operation is using OAuth security with permitted scopes of read and write pets.