Home > Software design >  Using Vert.x `AuthenticationHandler` from vertx-web, we hit the authentication provider for every ca
Using Vert.x `AuthenticationHandler` from vertx-web, we hit the authentication provider for every ca

Time:11-29

I'm using Vert.x for my web service, where a part of it required authorization. I've set an AuthenticationHandler (using the OAuth2 implementation from vertx-auth-oath2) to listen on the protected paths (lets say "/*") and it is correct called, sends a redirect to the authentication provider, which redirects back and then correctly to the real handler. This works fine.

But the next time we call the protected endpoint - it does the whole thing again. I see that in the abstract AuthenticationHandlerImpl class it checks if the context already has a user() and if so - will not run the actual auth handler, which is the behavior I need - but it obviously doesn't happen because every call is a new request with a new RoutingContext.

What is the "correct" way to retain the User object across requests, so that the auth handler will be happy?

I'm guessing it has something to do with session storage but I've never used that - up until now I was using a custom "API key" style solution, and I'm trying to do this "The Right Way(tm)" in this new project.

I'm using the latest Vert.x 4.3.5.

CodePudding user response:

You will need CookieHandler and SessionHandler to store and handle session with user. This will work out of the box with provided vertx-auth-oath2.

Here is a simple example to get you started: https://github.com/vert-x3/vertx-examples/blob/master/web-examples/src/main/java/io/vertx/example/web/auth/Server.java

  • Related