【问题标题】:Quarkus/MicroProfile Rest Client Session CookieQuarkus/MicroProfile Rest 客户端会话 Cookie
【发布时间】:2022-01-08 00:05:19
【问题描述】:

我正在使用 Quarkus Rest Client 与外部服务通信,该服务使用两个 cookie 来验证所有请求。这两个 cookie 从身份验证 API 和每个后续 API 调用返回。有没有办法自动处理这些 cookie?目前,我从身份验证 API 的响应对象中获取 cookie,并使用 @CookieParam 在每个请求中手动发送它们。

【问题讨论】:

    标签: cookies quarkus quarkus-rest-client


    【解决方案1】:

    我没试过,但你不能这样做吗:

     //pseudo code !!!
    @RestClient
    public interface UsersClient {
    
      @POST
      String backendCall(@CookieParam("Token1") token1, @CookieParam("Token2") String token2)
    
      @POST
      Map<String,String> authenticate(String param)
    
       default String makeCall(String param) {
           var tokens = authenticate(param);
           return backendCall(tokens.get(0), tokens.get(1));
       }
    }
    

    从您的服务中注入这个 rest 客户端并调用 makeCall(...) 方法。这应该对您的服务器进行身份验证,并使用响应中的令牌并将它们作为 cookie 发送到后端调用。

    对代码中的任何错误表示歉意:我是用平板电脑编写的。但我希望这个想法很清楚。

    还可以查看 Microprofile Rest 客户端文档以获取更多信息:

    https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_definitions

    【讨论】:

    • 感谢您的回答,但这仅涵盖身份验证后的一次通话。它不会保存令牌以供以后调用。我正在考虑用一个单例范围的请求范围 bean 包装我的休息客户端,并将 cookie 保存在那里。
    猜你喜欢
    • 2020-02-15
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2021-12-17
    • 2021-01-20
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多