通过合并自定义 HTTP header 支持每个浏览器的多个会话
Supporting multiple sessions per browser by incorporating a custom HTTP header
在 this article 中,它解释了 Spring Session 如何通过合并第二个标识符来支持一个浏览器的多个会话。似乎默认情况下此标识符称为 _s
并且它是请求的 URL.
中的查询字符串
我正在尝试实现完全相同的功能,但我想使用自定义 HTTP header 而不是查询字符串。我对整个想法有点陌生,有人可以告诉我这是否可行以及从哪里开始吗?
查询字符串参数 _s
用于定位要用于给定请求的 session。在official documentation中,这个参数被称为session别名参数.
如果您通过 source code for Spring Session, you will notice that the task of extracting the session alias parameter is delegated to the HttpSessionManager
interface. In the current code base, this interface has only one implementation - CookieHttpSessionStrategy
,它会从查询字符串中提取参数(请参阅链接源代码中的第 183 行)。
因此,无法使用当前实现从 HTTP header 中提取参数值(因为唯一可用的实现只能从查询字符串中提取它)。
但是,您可以子类化 CookieHttpSessionStrategy
,重写 getCurrentSessionAlias
方法以从 header 中提取参数,并重写应用程序的默认 session 策略作为:
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new MyHttpSessionStrategy();
}
其中,MyHttpSessionStrategy
是您编写的自定义策略。
在 this article 中,它解释了 Spring Session 如何通过合并第二个标识符来支持一个浏览器的多个会话。似乎默认情况下此标识符称为 _s
并且它是请求的 URL.
我正在尝试实现完全相同的功能,但我想使用自定义 HTTP header 而不是查询字符串。我对整个想法有点陌生,有人可以告诉我这是否可行以及从哪里开始吗?
查询字符串参数 _s
用于定位要用于给定请求的 session。在official documentation中,这个参数被称为session别名参数.
如果您通过 source code for Spring Session, you will notice that the task of extracting the session alias parameter is delegated to the HttpSessionManager
interface. In the current code base, this interface has only one implementation - CookieHttpSessionStrategy
,它会从查询字符串中提取参数(请参阅链接源代码中的第 183 行)。
因此,无法使用当前实现从 HTTP header 中提取参数值(因为唯一可用的实现只能从查询字符串中提取它)。
但是,您可以子类化 CookieHttpSessionStrategy
,重写 getCurrentSessionAlias
方法以从 header 中提取参数,并重写应用程序的默认 session 策略作为:
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new MyHttpSessionStrategy();
}
其中,MyHttpSessionStrategy
是您编写的自定义策略。