JAX-RS、RestEasy:无会话 cookie

发布于 2025-01-01 17:10:51 字数 887 浏览 1 评论 0原文

伙计们,

过去,当您点击 servlet/jsp 时,应用程序服务器会自动启动会话。它将在第一个动态响应中放置一个会话 cookie,并进行全程跟踪。

我有一个休息后端,我注意到没有会话 cookie 被交易。因此,我手动添加代码来发送 JSESSIONID cookie:

@Context 
private HttpServletRequest httpRequest;
// ...
@GET
@Path( "/{rcpGuid}" )
public Response myMethod( ... )
{
    final HttpSession session = httpRequest.getSession();
    final String sSessionId = session.getId();
    ...
    return Response.status( Response.Status.SEE_OTHER ).
        location( redirectUrl ).cookie( new NewCookie( "JSESSIONID", sSessionId ) );
}

现在,这导致返回 JSESSIONID cookie 的 2 个副本,而之前没有 Set-Cookie 标头。这就是我现在在浏览器检查器中看到的内容:

Set-Cookie:JSESSIONID=sdm-Q1P6pRoQbKd4-9cJylGb; Path=/nn, JSESSIONID=sdm-Q1P6pRoQbKd4-9cJylGb; Version=1

只要它能工作,我不在乎。但不幸的是,当我的浏览器请求重定向到的 URL 时(请注意,响应是“SEE_OTHER”),该请求不包含会话 ID。这导致我的应用程序无法正常运行。

有什么见解吗?

Folks,

Used to be that when you hit a servlet/jsp, the app server would automatically start a session. It would put a session cookie in the first dynamic response that would get tracked throughout.

I have a rest backend and I notice that no session cookies are being traded. So I manually add code to send the JSESSIONID cookie:

@Context 
private HttpServletRequest httpRequest;
// ...
@GET
@Path( "/{rcpGuid}" )
public Response myMethod( ... )
{
    final HttpSession session = httpRequest.getSession();
    final String sSessionId = session.getId();
    ...
    return Response.status( Response.Status.SEE_OTHER ).
        location( redirectUrl ).cookie( new NewCookie( "JSESSIONID", sSessionId ) );
}

Now this is causing 2 copies of the JSESSIONID cookie being returned where before there was no Set-Cookie header. This is what I see now in my browser's inspector:

Set-Cookie:JSESSIONID=sdm-Q1P6pRoQbKd4-9cJylGb; Path=/nn, JSESSIONID=sdm-Q1P6pRoQbKd4-9cJylGb; Version=1

I don't care as long as this would work. But unfortunately, when my browser requests the URL being redirected to (notice that the response is "SEE_OTHER"), that request does not bear the session id. This causing my app to not function right.

Any insights?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒带 2025-01-08 17:10:51

过去,当您点击 servlet/jsp 时,应用程序服务器会自动启动会话。它将在第一个动态响应中放置一个会话 cookie,并进行全程跟踪。

在调用 httpRequest.getSession() 之前,应用服务器不会创建会话。

我有一个休息后端,我注意到没有会话 cookie 被交易。所以我手动添加代码来发送 JSESSIONID cookie:

在您的示例中,您调用 getSession() 并创建一个单独的 jsessionid cookie。这可以解释为什么你有两个饼干。如果您两者都不做,那么您根本就不会拥有 jessionid

Used to be that when you hit a servlet/jsp, the app server would automatically start a session. It would put a session cookie in the first dynamic response that would get tracked throughout.

The app server doesn't create a session until httpRequest.getSession() is called.

I have a rest backend and I notice that no session cookies are being traded. So I manually add code to send the JSESSIONID cookie:

In your example, you call getSession() and create a separate jsessionid cookie. That would explain why you have two cookies. If you do neither, you wont have a jessionid at all.

兔姬 2025-01-08 17:10:51

如果它是 REST 后端,那么您不应该拥有 JSESSIONID cookie 并将后端端点保持为无状态。

从您的一项 Web 服务获取结果所需的所有信息都应包含在对该 Web 服务的请求中。 Web 服务是幂等的。

If it is a REST back-end, so you should not have JSESSIONID cookie and keep your back-end endpoints as Stateless.

All informations required to get a result from one of your web service should be contained in the request to the web service. A web service is idempotent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文