如何向 Quarkus 应用程序添加 http 拦截器?
How can I add an http interceptor to a Quarkus application?
我想向我的 Quarkus 应用程序添加一个 HTTP 拦截器,这样我就可以拦截所有 HTTP 请求。
怎样才能做到这一点?
Quarkus 使用 RESTEasy 作为其 JAX-RS 引擎。这意味着您可以利用 RESTEasy 的所有功能,包括 Filters and Interceptors.
例如,要创建一个非常简单的安全机制,您需要做的就是添加如下代码:
@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 处理,则需要使用这些堆栈的过滤机制。
更新
将 RESTEasy Reactive 与 Quarkus 结合使用时,可以使用 @ServerRequestFilter
注释而不是实现 ContainerRequestFilter
。
有关详细信息,请参阅 this
我想向我的 Quarkus 应用程序添加一个 HTTP 拦截器,这样我就可以拦截所有 HTTP 请求。 怎样才能做到这一点?
Quarkus 使用 RESTEasy 作为其 JAX-RS 引擎。这意味着您可以利用 RESTEasy 的所有功能,包括 Filters and Interceptors.
例如,要创建一个非常简单的安全机制,您需要做的就是添加如下代码:
@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 处理,则需要使用这些堆栈的过滤机制。
更新
将 RESTEasy Reactive 与 Quarkus 结合使用时,可以使用 @ServerRequestFilter
注释而不是实现 ContainerRequestFilter
。
有关详细信息,请参阅 this