Ravat Tailor
Ravat Tailor

Reputation: 1263

Vertx Handler Called twice for authentication and headers

On every request from postman the Vertx Handler called twice.

I have two handler which will be called before call come to request-processing handler. One to set header and one to authenticate user but both two are calling twice.

@Log4j2
public class BaseResponseHandler implements Handler<RoutingContext> {

    @Override
    public void handle(RoutingContext context) {
        HttpServerResponse response = context.response();
        log.info("Inside BaseResponse Handler!");
        response.putHeader("Content-Security-Policy",
                "script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'")
                // do not allow proxies to cache the data
                .putHeader("Cache-Control", "no-store, no-cache")
                .putHeader("X-Content-Type-Options", "nosniff")
                .putHeader("Strict-Transport-Security", "XYZ")
                .putHeader("X-Download-Options", "XYZ")
                .putHeader("X-XSS-Protection", "XYZ")
                .putHeader("X-FRAME-OPTIONS", "XYZ");

        response.setChunked(true);
        context.next();
    }

}

The Snippet I have in httpServerVerticle

router.route().order(1).handler(new BaseResponseHandler());
router.route().order(0).handler(new AuthenticationHandler()::authenticate);

In logs I am getting

10:09:54.214 [vert.x-eventloop-thread-4] [%vcl] DEBUG *.handlers.TokenHandler - User is Authenticated : io.vertx.ext.auth.jwt.impl.JWTUser@7855ebfa 
10:09:54.215 [vert.x-eventloop-thread-4] [%vcl] INFO  *.handlers.BaseResponseHandler - Inside BaseResponse Handler!
10:09:54.243 [vert.x-eventloop-thread-4] [%vcl] DEBUG *.handlers.TokenHandler - User is Authenticated : io.vertx.ext.auth.jwt.impl.JWTUser@5fb3053a 
10:09:54.243 [vert.x-eventloop-thread-4] [%vcl] INFO  *.handlers.BaseResponseHandler - Inside BaseResponse Handler!

Upvotes: 0

Views: 484

Answers (1)

Paulo Lopes
Paulo Lopes

Reputation: 5801

From your log I see 2 user object instance id (from the hashcode after the class name) so I am guessing you are seeing indeed 2 requests. Which can be a preflight check and the second the real request.

Try to log the http method. If the first one is an OPTIONS then it is indeed a preflight check.

Upvotes: 0

Related Questions