Search code examples
vert.x

JWT Auth in Vert.x 5


I am using Vert.x 5.0.0 SNAPSHOT in a new project. We have code in previous Vert.x v4.4.3 projects to add JWT authorization to a router built from an OpenAPI spec:

RouterBuilder
   .create(this.vertx, OPENAPI_URL)
   .compose(routerBuilder -> {

      // AUTHENTICATION & AUTHORIZATION
      routerBuilder.securityHandler("bearerAuth", Config.jwt().getAuthHandler());

      //...

But in 5.0.0 there's only a security method that receives a String and returns a Security object. That security object then accepts a handler and returns a RouterBuilder again:

router
   .route("/v1/*")
   .subRouter(routerBuilder
      .security("bearerAuth")
      .httpHandler(Config.jwt().getAuthHandler())
      .createRouter()
   );

But when I run it, it throws an exception on the .security("bearerAuth") line:

java.lang.NullPointerException: 'securityScheme' cannot be null

I can't find documentation or examples on how to do this. Any help will be appreciated!


Solution

  • Solved. The OpenAPI spec needs to have the "bearerAuth" security scheme, and we were loading multiple files and it wasn't on every one of them.