是否有一种优雅的方法来退出客户端雷克斯特福特?

发布于 2025-01-17 08:14:36 字数 668 浏览 3 评论 0原文

我实现了一个 ClientRequestFilter。但是客户端的一次调用不应被过滤,这意味着如果请求来自此类(在我的例子中,该类称为 TokenClient),则该方法应该只返回。现在,如您所见,我检查路径,如果它包含 /token,它将返回。但我宁愿检查该类是否是 ofInstance TokenClient。我怎样才能做到这一点?

@Provider
public class MyClientRequestFilter implements ClientRequestFilter {

    @Inject
    MyClient myClient;

    @Override
    public void filter(ClientRequestContext clientRequestContext) throws IOException {

        if(clientRequestContext.getUri().getPath().contains("/token"))
        {
            return;
        }

        String token= myClient.getToken();

        clientRequestContext.getHeaders().addFirst("Authorization", "Bearer "+token);
    }
}

I implemented a ClientRequestFilter. But one call of a client should not be filtered, which means if the request comes from this class (in my case the class is called TokenClient) the method should just return. Right now as you can see I check the path and if it contains /token it will return. But I would rather check if the class is ofInstance TokenClient. How can I do that ?

@Provider
public class MyClientRequestFilter implements ClientRequestFilter {

    @Inject
    MyClient myClient;

    @Override
    public void filter(ClientRequestContext clientRequestContext) throws IOException {

        if(clientRequestContext.getUri().getPath().contains("/token"))
        {
            return;
        }

        String token= myClient.getToken();

        clientRequestContext.getHeaders().addFirst("Authorization", "Bearer "+token);
    }
}

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

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

发布评论

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

评论(1

小忆控 2025-01-24 08:14:36

有一种方法可以知道它所描述的方法 此处

本质上你做了类似的事情:

@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {

    Method targetMethod = (Method)clientRequestContext.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

    // check target method
    
}

There is a way to know which method and it described here.

Essentially you do something like:

@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {

    Method targetMethod = (Method)clientRequestContext.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

    // check target method
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文