我可以将敏感数据存储在 Quarkus 应用程序的 Vert.x 上下文中吗?

发布于 2025-01-09 09:09:37 字数 216 浏览 3 评论 0原文

我正在寻找一个地方来存储一些请求范围的属性,例如使用 Quarkus 请求过滤器的用户 id。我稍后想在日志处理程序中检索这些属性并将它们放入 MDC 日志记录上下文中。

Vertx.currentContext() 是放置此类请求属性的正确位置吗?或者我在此上下文中设置的属性可以被其他请求读取吗?

如果这不是存储此类数据的正确位置,那么哪里才是正确的位置呢?

I am looking for a place to store some request scoped attributes such as user id using a Quarkus request filter. I later want to retrieve these attributes in a Log handler and put them in the MDC logging context.

Is Vertx.currentContext() the right place to put such request attributes? Or can the properties I set on this context be read by other requests?

If this is not the right place to store such data, where would be the right place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2025-01-16 09:09:37

是...和否:-D
Vertx.currentContext() 可以提供两种类型的对象:

  • 在此事件循环上执行的所有并发处理之间共享根上下文(因此不共享数据)
  • 复制上下文,这些上下文对于处理及其处理而言是本地的延续(你可以在这些中分享)

在 Quarkus 2.7.2 中,我们做了很多工作来改进对重复上下文的支持。以前,它们仅用于 HTTP,现在用于 gRPC 和 @ConsumeEvent。 Quarkus 2.8 中将支持 Kafka 和 AMQP。

此外,在 Quarkus 2.7.2 中,我们引入了两个可能有用的新功能:

  1. 您无法将数据存储在根上下文中。我们会为您检测到这一点并抛出 UnsupportedOperationException。原因是安全。
  2. 我们引入了一个新的实用程序类(io.smallrye.common.vertx.ContextLocals 来访问上下文局部变量

这是一个简单的示例:

 AtomicInteger counter = new AtomicInteger();
public Uni<String> invoke() {
    Context context = Vertx.currentContext();

    ContextLocals.put("message", "hello");
    ContextLocals.put("id", counter.incrementAndGet());       

    return invokeRemoteService()
       // Switch back to our duplicated context:
        .emitOn(runnable -> context.runOnContext(runnable))
        .map(res -> {
            // Can still access the context local data
            String msg = ContextLocals.<String>get("message").orElseThrow();
            Integer id = ContextLocals.<Integer>get("id").orElseThrow();
            return "%s - %s - %d".formatted(res, msg, id);
        });
}

Yes ... and no :-D
Vertx.currentContext() can provide two type of objects:

  • root context shared between all the concurrent processing executed on this event loop (so do NOT share data)
  • duplicated contexts, which are local to the processing and its continuation (you can share in these)

In Quarkus 2.7.2, we have done a lot of work to improve our support of duplicated context. While before, they were only used for HTTP, they are now used for gRPC and @ConsumeEvent. Support for Kafka and AMQP is coming in Quarkus 2.8.

Also, in Quarkus 2.7.2, we introduced two new features that could be useful:

  1. you cannot store data in a root context. We detect that for you and throw an UnsupportedOperationException. The reason is safety.
  2. we introduced a new utility class (io.smallrye.common.vertx.ContextLocals to access the context locals.

Here is a simple example:

 AtomicInteger counter = new AtomicInteger();
public Uni<String> invoke() {
    Context context = Vertx.currentContext();

    ContextLocals.put("message", "hello");
    ContextLocals.put("id", counter.incrementAndGet());       

    return invokeRemoteService()
       // Switch back to our duplicated context:
        .emitOn(runnable -> context.runOnContext(runnable))
        .map(res -> {
            // Can still access the context local data
            String msg = ContextLocals.<String>get("message").orElseThrow();
            Integer id = ContextLocals.<Integer>get("id").orElseThrow();
            return "%s - %s - %d".formatted(res, msg, id);
        });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文