【问题标题】:Vertx Handler Called twice for authentication and headersVertx 处理程序为身份验证和标头调用了两次
【发布时间】:2020-04-12 04:51:09
【问题描述】:

对于邮递员的每个请求,Vertx 处理程序都会调用两次。

我有两个处理程序,它们将在调用请求处理处理程序之前被调用。 一个用于设置标头,一个用于验证用户,但两者都调用了两次。

@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();
    }

}

我在 httpServerVerticle 中的片段

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

在我得到的日志中

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!

【问题讨论】:

    标签: vert.x


    【解决方案1】:

    从您的日志中,我看到了 2 个用户对象实例 ID(来自类名后面的哈希码),所以我猜您确实看到了 2 个请求。这可以是一个预检检查,第二个是真正的请求。

    尝试记录http方法。如果第一个是 OPTIONS,那么它确实是一个预检。

    【讨论】:

    • 嗨,Paulo,我正在打邮递员的电话,我不确定邮递员是否进行了飞行前检查
    • 可以记录请求http方式。如果是 OPTIONS 请求,则为预检调用。
    • 在这两种情况下 HttpMethod 都是 POST
    • 路由器 subRouter = router.mountSubRouter("/v2/api", router);这条线产生了一个问题,如果我删除 subRouter 它只调用一次身份验证,有什么原因吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2015-09-09
    相关资源
    最近更新 更多