简体   繁体   English

如何从Rest API登录的泽西岛客户端响应中获取Cookie到期日期?

[英]How to get cookie expiry date from jersey client response from rest api login?

If you hit Jira rest api for login from Postman tool is displaying cookies with expiry date properly but if you hit from java jersey rest client, its not showing expiry dates for the respective cookies. 如果您从Postman工具中点击了Jira rest api进行登录,则显示的cookie的到期日期是正确的,但是如果您是从java jersey rest客户端中单击的,则它不会显示相应cookie的到期日期。

JIRA crowd sso setup version : v7.12.1 JIRA人群sso设置版本:v7.12.1

Jersey api 1.x 泽西岛API 1.x

The below are the Postman response cookies: and Jersey java client response cookies. 以下是邮递员响应cookie:和Jersey Java客户端响应cookie。

postman - https://i.imgur.com/teIMNFJ.png 邮递员-https: //i.imgur.com/teIMNFJ.png
jersey client - https://i.stack.imgur.com/KuU8q.png 泽西岛客户-https: //i.stack.imgur.com/KuU8q.png

Can anyone tell me how to get the cookies values same as postman is showing with expiry dates using java rest client ? 谁能告诉我如何使用Java rest客户端获取与邮递员显示的带有过期日期相同的cookie值? why the response is different in two places ? 为什么两个地方的反应不同? what java api i need to use to get full details of cookies from java program ? 我需要使用什么Java API从Java程序获取Cookie的完整详细信息?

Welcome :) 欢迎:)

You can use getExpiryDate() in Java on Cookie Objects. 您可以在Java中对Cookie对象使用getExpiryDate() Return type is Date. 返回类型为日期。

When you are using the JIRA rest api for login , as per the documentation a session cookie is returned. 当您使用JIRA rest api进行登录时,根据文档,将返回session cookie

More importantly, you will get the session cookie (in the header of the response) from the server, which you can use in subsequent requests. 更重要的是,您将从服务器获取会话cookie(在响应的标头中),可以在后续请求中使用它。 You can see an example of this below. 您可以在下面看到一个示例。 You'll notice that the cookie name and value are the same as the cookie name and value in the session response above. 您会注意到cookie名称和值与上面会话响应中的cookie名称和值相同。

Session cookies does not have a expiry-time set. 会话Cookie没有设置有效时间。 The cookies expire when the session is ended. 当会话结束时,cookie过期。 For eg., In browsers when a window is closed - the session cookies will be removed. 例如,在浏览器中,当窗口关闭时-会话cookie将被删除。

Read more about session cookies here or google it. 在此处阅读更多有关会话Cookie的信息,或在Google上对其进行搜索。

The documentation also states this 该文档还指出了这一点

Cookie expiration Cookie到期

One disadvantage of using cookies compared to basic authorization is that they expire. 与基本授权相比,使用cookie的一个缺点是它们已过期。 You have probably noticed this when accessing Jira through a web browser. 通过网络浏览器访问Jira时,您可能已经注意到了这一点。 Every once in a while, especially if you have not used Jira in a while, you need to log in again because your cookie has expired. 偶尔(尤其是如果您有一段时间没有使用Jira的话),由于您的cookie已过期,因此需要再次登录。 The same occurs when using REST. 使用REST时也会发生同样的情况。 If you write a script or code that involves REST API calls and: 如果您编写的脚本或代码涉及REST API调用,并且:

It only runs for a few minutes, then you should not worry about cookies expiring. 它仅运行几分钟,因此您不必担心Cookie过期。 It runs for a longer period of time due to more complex integration activities, then expiring cookies may cause problems. 由于更复杂的集成活动,它会运行更长的时间,然后过期的cookie可能会引起问题。 If you use REST with a cookie that has expired, you will receive a 401 error response from Jira. 如果将REST与已过期的cookie一起使用,您将收到Jira的401错误响应。 The response body will contain a message telling you that your cookie is invalid. 响应正文将包含一条消息,告诉您您的cookie无效。 At that point, you will need to re-authenticate to the session resource on the "auth" API. 此时,您将需要在“ auth” API上对会话资源重新进行身份验证。

So you might need to consider handling 401 errors. 因此,您可能需要考虑处理401错误。

As mentioned, If postman is giving future date then it may be not a session cookie :-). 如前所述,如果邮递员给出了将来的日期,则它可能不是会话cookie :-)。 Then you should be able to get it like 那你应该能够像

ClientResponse response = //code to execute request REST
List<NewCookie> cookies = response.getCookies();
Date expires = cookies.get(0).getExpiry();
//OR
int maxAge =cookies.get(0).getMaxAge()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM