如何向 Quarkus 应用程序添加 http 拦截器? [英] How can I add an http interceptor to a Quarkus application?

查看:57
本文介绍了如何向 Quarkus 应用程序添加 http 拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Quarkus 应用程序中添加一个 HTTP 拦截器,以便我可以拦截所有 HTTP 请求.怎样才能做到这一点?

I would like to add an HTTP interceptor to my Quarkus application so I can intercept all HTTP requests. How can such that be achieved?

推荐答案

Quarkus 使用 RESTEasy 作为其 JAX-RS 引擎.这意味着您可以利用 RESTEasy 的所有功能,包括 过滤器和拦截器.

Quarkus uses RESTEasy as its JAX-RS engine. That means that you can take advantage of all of RESTEasy's features, including Filters and Interceptors.

例如,要创建一个非常简单的安全机制,您只需添加如下代码:

For example to create a very simple security mechanism, all you would need to do is add code like the following:

@Provider
public class SecurityInterceptor implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext context) {
        if ("/secret".equals(context.getUriInfo().getPath())) {
            context.abortWith(Response.accepted("forbidden!").build());
        }
    }
}

需要注意的是,这仅适用于 Quarkus 中由 JAX-RS 处理的请求.如果请求由纯 Vert.x 或 Undertow 处理,则需要使用这些堆栈的过滤机制.

It should be noted that this only works for requests that are handled by JAX-RS in Quarkus. If the requests are handled by pure Vert.x or Undertow, the filtering mechanisms of those stacks will need to be used.

更新

当在 Quarkus 中使用 RESTEasy Reactive 时,可以使用 @ServerRequestFilter 注释来代替实现 ContainerRequestFilter.请参阅了解更多信息

When using RESTEasy Reactive with Quarkus, the @ServerRequestFilter annotation can be used instead of implementing ContainerRequestFilter. See this for more information

这篇关于如何向 Quarkus 应用程序添加 http 拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆