简体   繁体   English

Servlet充当代理:如何转发会话?

[英]Servlet Acting as Proxy: How to forward the session?

I am not too sure about the feasibility of the requirement that I am trying to achieve, but here is how it goes: 我不太确定我要实现的要求的可行性,但是这是如何进行的:

  • I have created a Servlet that acts as proxy. 我创建了一个充当代理的Servlet。 It receives a RESTful call and then invokes another RESTful service on a remote server (node). 它收到一个RESTful调用,然后在远程服务器(节点)上调用另一个RESTful服务。
  • The forwarding is achieved via HTTPClient and not with a Request Dispatcher. 转发是通过HTTPClient实现的,而不是通过Request Dispatcher实现的。 I basically issue a new HTTP request to the remote server. 我基本上是向远程服务器发出新的HTTP请求。
  • When the first server (proxy server) receives the call, the request ( HttpServletRequest ) has a session associated with it. 当第一个服务器(代理服务器)接收到呼叫时,请求( HttpServletRequest )具有与其相关联的会话。 The isNew() property of the HTTPSession is false. HTTPSessionisNew()属性为false。
  • When the call is forwarded and the remote server receives the call, the session gets to be a brand new one. 当呼叫转移并且远程服务器接收到呼叫时,会话将成为一个全新的会话。

I am trying to basically find a way to forward the session to the remote server as well. 我试图从根本上找到一种将会话转发到远程服务器的方法。

To be more precise: Is it possible simply to get a session from a HttpServletRequest and put it into a session of a newly created HTTP request (via HTTPClient )? 更精确地说: 是否可以简单地从HttpServletRequest获取会话并将其放入新创建的HTTP请求的会话中(通过HTTPClient )?

It depends on how your remote WS maintains a session. 这取决于您的远程WS如何维护会话。 If for example, it uses cookies (Tomcat does that amongst other techniques), then forwarding the incoming headers should help you achieve that (make sure that you mention that you accept cookies, but I thing HTTPClient does it by default). 例如,如果它使用cookie(Tomcat会在其他技术中做到这一点),则转发传入的标头应该可以帮助您实现这一点(请确保您提到接受cookie,但我默认HTTPClient会这样做)。 Now, if it is based on parameter in the URL, then you should try to reproduce that behaviour. 现在,如果它基于URL中的参数,那么您应该尝试重现该行为。

If the two nodes (proxy and service) are separate processes that are not part of an application cluster, then probably not. 如果两个节点(代理节点和服务节点)是不属于应用程序集群的单独进程,则可能不是。

The servlet container typically manages the HttpSession. Servlet容器通常管理HttpSession。 If you are forwarding a request to another service, hosted by a different container, then you will have a different session object. 如果将请求转发到由另一个容器托管的另一个服务,则将具有另一个会话对象。

If the two nodes are part of a cluster, then typically the session can be shared between nodes in the cluster via a number of mechanisms (in memory replication, database synchronization, etc). 如果两个节点是群集的一部分,那么通常可以通过多种机制(在内存复制,数据库同步等中)在群集中的节点之间共享会话。

Another option, is to externalize your session data into something like Redis, Memcached, Coherence, etc. Some application servers have pluggable support for such a process. 另一个选择是将会话数据外部化为Redis,Memcached,Coherence等。某些应用程序服务器对此过程具有可插拔的支持。 I believe in this scenario the application server nodes would not necessarily have to be party of a cluster to share the session data. 我相信在这种情况下,应用程序服务器节点不一定必须是集群的成员才能共享会话数据。

I lacked some of the fundamentals of session handling when I asked this question. 当我问这个问题时,我缺乏会话处理的一些基础知识。 After some research and discussion here is what I got: 经过一番研究和讨论之后,我得到了:

  • Basically, the sessions are handled by a JSESSIONID variable. 基本上,会话由JSESSIONID变量处理。
  • JSESSIONID is created automatically (can be turned off) when the request hits the server. 当请求到达服务器时,将自动创建JSESSIONID (可以将其关闭)。
  • It gets sent back with in the response header. 它通过响应头发送回。
  • The reason why the remote server thinks it is a new session is because the request doesn't have this JSESSIONID set. 远程服务器认为这是一个新会话的原因是因为请求没有设置此JSESSIONID

  • The proxy does the following to ensure that the session is forwarded: 代理执行以下操作以确保转发会话:

    1. When the incoming request comes in; 当传入请求进入时; create and store the JSESSIONID . 创建并存储JSESSIONID
    2. Issue a new request to the remote server. 向远程服务器发出新请求。
    3. Unpack the response header from the remote server and extract JSESSIONID . 从远程服务器解压缩响应头,然后提取JSESSIONID
    4. Maintain a mapping of client side JSESSIONID and remote server JSESSIONID . 维护客户端JSESSIONID和远程服务器JSESSIONID的映射。
    5. For any of the following request, use this mapping to send requests to the remote server. 对于以下任何请求,请使用此映射将请求发送到远程服务器。

Basically, the proxy does the mapping from the client side's JSESSIONID to the remote server's JSESSIONID . 基本上,代理会执行从客户端的JSESSIONID到远程服务器的JSESSIONID的映射。 This way, the session gets forwarded to the remote server. 这样,会话将转发到远程服务器。

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

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