从 java VM 调用设置 cookie 的 REST 服务时,cookie 如何工作?
How does cookies work, when calling cookie-setting REST services from java VM?
我正在学习如何使用 HP Quality Center 的 REST api 来查询和操作数据。与 REST 标准不同,这个 API 不是完全无状态的。它使用 cookie 来存储身份验证会话。
我尝试使用 Jersey Client 库实现一个非常简单的测试。我可以通过发送我的凭据成功验证我的用户。 API 参考声称这将设置一个 cookie,我很乐意继续调用 REST api。但是,一个简单的 "is-authenticated" 调用 returns 401,身份验证失败。
我感觉 cookie 的写入或读取工作不正常,而其他一切似乎都在正常工作。但是,在不涉及浏览器的情况下,我无法确定是否或如何设置和读取 cookie。那么,当从 java VM 调用设置 cookie 的 REST 服务时,cookie 是如何工作的?它真的有效吗?它们存储在哪里?
我正在使用 Eclipse Kepler 作为我的 IDE,如果这很重要的话,还有 32 位 java 1.6 JDK 和 JRE。
下面的代码和响应字符串:
1.正在登录:
Client client = ClientBuilder.newClient();
Response response = client
.target("http://[host]:[port]").path("qcbin/authentication-
point/alm-authenticate")
.request().post(Entity.entity("<alm-authentication>
<user>username</user>
<password>secret</password></alm-authentication>",
MediaType.TEXT_XML_TYPE));
System.out.println(response.toString());
输出:
InboundJaxrsResponse{ClientResponse{method=POST,
uri=http://[host]:[port]/qcbin/authentication-point/alm-authenticate,
status=200, reason=OK}}
API Return 描述:
One of:
HTTP code 200 and sets the LWSSO cookie (LWSSO_COOKIE_KEY).
HTTP code 401 for non-authenticated request. Sends header
WWW-Authenticate: ALMAUTH
2。验证登录:
response = client.target("http://[host]:[port]")
.path("qcbin/rest/is-authenticated")
.request().get();
System.out.println(response.toString());
输出:
InboundJaxrsResponse{ClientResponse{method=GET,
uri=http://[host]:[port]/rest/is-authenticated, status=401,
reason=Authentication failed. Browser based integrations - to login append
'?login-form-required=y to the url you tried to access.}}
PS: 将 ?login-form-required=y
添加到 URL,将在调用时显示登录 window在浏览器中,但不在此处。将该行附加到 URL 实际上仍然给出相同的错误消息,并建议再次附加它。此外,在浏览器中调用时,已验证 returns 200,成功,即使没有登录表单。
当您登录时,您会得到一个 cookie,它是一个名称加上一个值。
REST 服务器希望您在发出的每个请求 header 中传递此信息。
看看 client.request()
得到的 object;应该有一种方法可以指定额外的 header 发送到服务器。 header 名称必须是 Cookie
并且 header 值必须是 name=value
.
因此,如果服务器响应一个名为 sessionID
且值为 1234
的 cookie,那么您需要如下内容:
client.request().header("Cookie", "sessionID=1234")
相关:
我正在学习如何使用 HP Quality Center 的 REST api 来查询和操作数据。与 REST 标准不同,这个 API 不是完全无状态的。它使用 cookie 来存储身份验证会话。
我尝试使用 Jersey Client 库实现一个非常简单的测试。我可以通过发送我的凭据成功验证我的用户。 API 参考声称这将设置一个 cookie,我很乐意继续调用 REST api。但是,一个简单的 "is-authenticated" 调用 returns 401,身份验证失败。
我感觉 cookie 的写入或读取工作不正常,而其他一切似乎都在正常工作。但是,在不涉及浏览器的情况下,我无法确定是否或如何设置和读取 cookie。那么,当从 java VM 调用设置 cookie 的 REST 服务时,cookie 是如何工作的?它真的有效吗?它们存储在哪里?
我正在使用 Eclipse Kepler 作为我的 IDE,如果这很重要的话,还有 32 位 java 1.6 JDK 和 JRE。
下面的代码和响应字符串:
1.正在登录:
Client client = ClientBuilder.newClient();
Response response = client
.target("http://[host]:[port]").path("qcbin/authentication-
point/alm-authenticate")
.request().post(Entity.entity("<alm-authentication>
<user>username</user>
<password>secret</password></alm-authentication>",
MediaType.TEXT_XML_TYPE));
System.out.println(response.toString());
输出:
InboundJaxrsResponse{ClientResponse{method=POST,
uri=http://[host]:[port]/qcbin/authentication-point/alm-authenticate,
status=200, reason=OK}}
API Return 描述:
One of:
HTTP code 200 and sets the LWSSO cookie (LWSSO_COOKIE_KEY).
HTTP code 401 for non-authenticated request. Sends header WWW-Authenticate: ALMAUTH
2。验证登录:
response = client.target("http://[host]:[port]")
.path("qcbin/rest/is-authenticated")
.request().get();
System.out.println(response.toString());
输出:
InboundJaxrsResponse{ClientResponse{method=GET,
uri=http://[host]:[port]/rest/is-authenticated, status=401,
reason=Authentication failed. Browser based integrations - to login append
'?login-form-required=y to the url you tried to access.}}
PS: 将 ?login-form-required=y
添加到 URL,将在调用时显示登录 window在浏览器中,但不在此处。将该行附加到 URL 实际上仍然给出相同的错误消息,并建议再次附加它。此外,在浏览器中调用时,已验证 returns 200,成功,即使没有登录表单。
当您登录时,您会得到一个 cookie,它是一个名称加上一个值。
REST 服务器希望您在发出的每个请求 header 中传递此信息。
看看 client.request()
得到的 object;应该有一种方法可以指定额外的 header 发送到服务器。 header 名称必须是 Cookie
并且 header 值必须是 name=value
.
因此,如果服务器响应一个名为 sessionID
且值为 1234
的 cookie,那么您需要如下内容:
client.request().header("Cookie", "sessionID=1234")
相关: