简体   繁体   English

如何跨多台服务器管理用户会话?

[英]How to manage user sessions across multiple servers?

I have a web service which authenticates a user, and if successful gives them a JSON Web Token which they can use to persist their session. 我有一个对用户进行身份验证的Web服务,如果成功,则为他们提供一个JSON Web令牌,他们可以使用它们来持久化会话。
Currently I only have one server, but would ideally like to increase that to two or more for the sake of performance, however I'm unsure as to how that'll work with different servers as they'll have different instances of maintaining who is currently logged in. 目前,我只有一台服务器,但是出于性能考虑,希望将其增加到两台或更多,但是我不确定该如何与不同的服务器一起使用,因为它们将具有维护谁的身份的不同实例。当前登录。

For example, my service will examine the JWT the client returns, and (assuming it's a good token), will take the user ID and session and check if there is a user logged in with that session ID. 例如,我的服务将检查客户端返回的JWT,并(假设它是一个很好的令牌)将获取用户ID和会话,并检查是否有使用该会话ID登录的用户。
However, if for example, the middleware that precedes the web service layer determines that the server the user was initially logged in is currently unavailable and routes the request to a different server instance of the web service, how is the authentication controller meant to evaluate if the request is valid? 但是,例如,如果位于Web服务层之前的中间件确定用户最初登录的服务器当前不可用,并将请求路由到Web服务的其他服务器实例,那么身份验证控制器应如何评估是否该请求有效吗?

A very basic version of my service can be found below, with the black arrows representing ordinary communication, and the red arrows representing communication in the event that Layer One decides to route the request to Instance Two. 我的服务的基本版本可以在下面找到,黑色箭头表示普通通信,红色箭头表示通信,如果第一层决定将请求路由到实例2。 Layer One, in essence is very basic and doesn't really do anything other than route traffic to the next layer dependent on the availability of the layer two instances. 第一层本质上是非常基础的,除了根据第二层实例的可用性将流量路由到下一层之外,它实际上没有做任何事情。

在此处输入图片说明

One of the main advantages of JWT is that it does not need server storage because it is self-contained and stateless. JWT的主要优点之一是它不需要服务器存储,因为它是独立的且无状态的。 If you need server sessions may be JWT it is not your best option to manage authentication. 如果您需要服务器会话可能是JWT,那么管理身份验证不是最佳选择。 See Stop using JWT for sessions 请参阅停止使用JWT进行会话

Currently application servers use the SESSION ID to manage load balancing and redirection to the server that store the user session, including persistent storage and migration of session. 当前,应用程序服务器使用SESSION ID来管理负载平衡和重定向到存储用户会话的服务器,包括持久性存储和会话迁移。 Using a JWT you won't take profit of these characteristics. 使用JWT,您将不会从这些特性中受益。

Use JWT if you need a REST stateless endpoint with authentication or you need to exchange data securely between parties 如果您需要具有身份验证的REST无状态端点,或者需要在各方之间安全地交换数据,请使用JWT

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

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