Java 如何使用 ContainerRequestContext 从 HTTP 请求中获取 JSESSIONID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34989456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the JSESSIONID from a HTTP request using ContainerRequestContext?
提问by Imran khan
I have the following HTTP headers in a request and I want to extract the JSESSIONID
from it:
我在请求中有以下 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=NFA_QA/34 CFNetwork/720.5.7 Darwin/14.5.0 (x86_64)
I'm using a ContainerRequestContext
as following:
我正在使用ContainerRequestContext
如下:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}
Getting result as:
得到结果为:
JSESSIONID=zv71od6l2fd41hv6yf0980khy
And:
和:
@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);
}
Getting result as:
得到结果为:
Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}
What is the best way to extract the JSESSIONID
from the request?
JSESSIONID
从请求中提取 的最佳方法是什么?
采纳答案by cassiomolin
A step back
退一步
First of all, REST and session identifiers don't sound well in the same sentence.
首先,REST 和会话标识符在同一个句子中听起来不太好。
The Sin RESTmeans statelessand not stateful. In REST applications, the session state must be managed by the client and not by the server. Consequently, there must not be any session identifiers.
在小号在REST手段无国籍,而不是状态。在 REST 应用程序中,会话状态必须由客户端管理,而不是由服务器管理。因此,不得有任何会话标识符。
For more information, have a look hereand here.
Getting a value from a cookie in JAX-RS
从 JAX-RS 中的 cookie 中获取值
I think you are looking for the following solution:
我认为您正在寻找以下解决方案:
Cookie cookie = requestContext.getCookies().get("JSESSIONID");
String value = cookie.getValue();
For more information about Cookie
, have a look at the documentation.