【问题标题】:How to get the JSESSIONID from a HTTP request using ContainerRequestContext?如何使用 ContainerRequestContext 从 HTTP 请求中获取 JSESSIONID?
【发布时间】:2016-05-01 13:50:33
【问题描述】:

我的请求中有以下 HTTP 标头,我想从中提取 JSESSIONID:

Accept=application/json
Accept-Encoding=gzip
deflate,Accept-Language=en-us
Connection=keep-alive
Content-Length=0
Content-Type=application/json
Cookie=JSESSIONID=ss0ox8w99o9142b73rssvc0r
Host=localhost:8080
User-Agent=Test_QA/34 CFNetwork/711.5.7 Darwin/14.5.0 (x86_64)

我正在使用ContainerRequestContext,如下所示:

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}

得到结果为:

JSESSIONID=zv71od6l2fd41hv6yf0980khy

还有:

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    Map<String, javax.ws.rs.core.Cookie> clientCookie = requestContext.getCookies();
    System.out.println("Client Cookie Map: " + clientCookie);
}

得到结果为:

Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}

从请求中提取JSESSIONID 的最佳方法是什么?

【问题讨论】:

    标签: java rest jax-rs


    【解决方案1】:

    退后一步

    首先,REST 和会话标识符在同一个句子中听起来不太好。

    REST 中的S 表示无状态 而不是有状态。在 REST 应用程序中,会话状态必须由客户端而非服务器管理。因此,不能有任何会话标识符。

    如需了解更多信息,请查看here 和here。

    从 JAX-RS 中的 cookie 中获取值

    我认为您正在寻找以下解决方案:

    Cookie cookie = requestContext.getCookies().get("JSESSIONID");
    String value = cookie.getValue();
    

    有关Cookie 的更多信息,请查看documentation。

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2018-04-13
      • 2023-02-10
      • 1970-01-01
      • 2017-03-29
      • 2017-11-15
      • 2013-10-19
      • 1970-01-01
      • 2020-07-20
      相关资源
      最近更新 更多