我想在我的 Quarkus 应用程序中添加一个 HTTP 拦截器,以便我可以拦截所有 HTTP 请求。怎样才能做到这一点?
geo*_*and 10
Quarkus 使用 RESTEasy 作为其 JAX-RS 引擎。这意味着您可以利用 RESTEasy 的所有功能,包括Filters 和 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
应该注意的是,这仅适用于 Quarkus 中由 JAX-RS 处理的请求。如果请求由纯 Vert.x 或 Undertow 处理,则需要使用这些堆栈的过滤机制。
更新
将 RESTEasy Reactive 与 Quarkus@ServerRequestFilter一起使用时,可以使用注释而不是实现ContainerRequestFilter. 请参阅此了解更多信息
| 归档时间: |
|
| 查看次数: |
5294 次 |
| 最近记录: |