通过注释在 Quarkus 中进行转发(启用客户端路由)的请求或响应过滤器

问题描述 投票:0回答:1

我使用 Quarkus (3.4.3) 来托管我的后端休息端点以及我的 Angular SPA,这只是一个非常简单的接口,可以与这些休息端点实际交互。

我的问题实际上是一个很常见的问题。我需要客户端转发才能工作,以便我可以直接访问,例如https://主机:8080/home。

我知道,由于服务器上没有 home 资源,我需要将请求重定向到 index.html (https://host:8080/),同时将 home 部分保留在 URL 中,以便客户端转发到发生。

作为一种解决方案,我想通过注释使用请求或响应过滤器(如https://quarkus.io/guides/resteasy-reactive中所述)。我还找到了使用 ContainerResponseFilter 的另一个解决方案(https://marcelkliemannel.com/articles/2022/routes-forwarding-for-javascript-frontends-in-quarkus/)。

不幸的是,我无法使用 ContainerResponseFilter 解决方案,因为 HttpResponse 转发部分似乎已过时/不正确(这些方法在任何可用的 HttpResponse 类上都不存在)。

无论如何,我更愿意使用注释方法,但我就是找不到正确实现它的方法。

当我尝试 @ServerRequestFilter(preMatching = true) 时,问题是我不知道如何重定向/转发用户(requestContext.setRequestUri 不执行任何操作)。

@ServerRequestFilter(preMatching = true)
    public void preMatchingFilter(ContainerRequestContext requestContext) {
        URI newLocation = URI.create("http://host:8080/");
        requestContext.setRequestUri(newLocation);
    }

使用@ServerResponseFilter,问题是此时响应已经被写入。

有人可以向我展示一个非常简单的示例,说明如何将请求从 https://host:8080/home 转发到 https://host:8080/,同时保留路径 /home 以便进行客户端路由吗?

angular routes quarkus vert.x
1个回答
0
投票

@ServerResponseFilter 可用于更改响应。例如,这将创建一个响应,告诉浏览器重定向:

@ServerResponseFilter
public void forwardingFilter(ContainerRequestContext requestContext, 
                             ContainerResponseContext responseContext){

    URI newLocation = URI.create("http://host:8080/");
    responseContext.getHeaders().putSingle("Location", newLocation);
    responseContext.setStatus(Response.Status.TEMPORARY_REDIRECT.getStatusCode());
}

我希望这适用于您的场景。

© www.soinside.com 2019 - 2024. All rights reserved.