java - 显式设置 JSESSIONID

标签 java session-variables jsessionid

所以,我正在构建一个应用程序,我希望用户能够向另一个用户发送链接并处理相同的数据。几乎不需要将数据保留超过 15 分钟,并且我希望应用程序不必依赖数据库。

我当前有一些变量存储在 HttpSession 中。

理想情况下,我希望能够在 URL 中设置参数“sid”,并使用它在第二个客户端的浏览器(接收带有 SID 的链接的浏览器)上设置 session ID。

不幸的是,一旦设置了当前 session 的 SID,似乎就无法更改它了。

到目前为止我想出的唯一解决方案是使用 HttpPost.setHeader("Cookie", "JSESSIONID="+ getSessionId()) 方法引用 doGet() ,并且以这种方式获取特定 session 的数据,但这看起来相当笨重。

有什么建议吗?

编辑

这似乎已经变成了关于设置两个用户具有相同 session 以便 session 变量可以保持相同是否明智的讨论。请注意,我正在寻找一种非数据库(即轻量级)解决方案。 PHP 允许您任意设置 session ID 来执行此操作,但在本例中我将部署到 Tomcat。允许轻量级共享一些参数的东西,就是这样......

然后,我所做的是使用 context.setAttribute() 设置上下文变量。丑陋如罪,但我只能想出办法,除非有人有更好的想法。

最佳答案

这种方法存在一些安全问题,因为您正在用它模拟[ session 劫持]1 :

In computer science, session hijacking, sometimes also known as cookie hijacking is the exploitation of a valid computer session—sometimes also called a session key—to gain unauthorized access to information or services in a computer system. In particular, it is used to refer to the theft of a magic cookie used to authenticate a user to a remote server. It has particular relevance to web developers, as the HTTP cookies used to maintain a session on many web sites can be easily stolen by an attacker using an intermediary computer or with access to the saved cookies on the victim's computer (see HTTP cookie theft).

更好的方法是以两个用户都可以从其 session 中访问数据的方式设置数据。如果您使用单个服务器,您可以使用服务器内的一些全局数据来完成此操作。在集群或云环境中,您将需要一些通用存储(即数据库、全局缓存等)

编辑

您在具有不同 session 的两个用户之间共享数据的方向是正确的。

There is one context per "web application" per Java Virtual Machine where an attribute allows a servlet container to give the servlet additional information.

属性旨在用于在同一 Web 应用程序的 servlet 之间共享容器内的数据。

对于集群:

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

关于java - 显式设置 JSESSIONID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723680/

相关文章:

java - NoSuchMethodError : org. springframework.data.util.TypeInformation.isSubTypeOf(Ljava/lang/Class;)Z

asp.net - 如何通过 SessionID 在没有 HttpContext.Current 的情况下获取 session 数据

session - Tomcat 中带有过期日期的 JSESSIONID Cookie

java - CAS 单点登录过滤器

java - 如何在文件传输时创建进度条

java - 文件名上的黄色背景在 Intellij 中意味着什么?

java - SessionAttribute 和 SpringFramework

php - 在 PHP 中使用 Perl CGI session

java - Apache Camel - JMS 远程处理 : Exception not thrown back to the producer