【问题标题】:Spring Session changed the cookie value causing HTTP 302 on succeeding requestsSpring Session 更改了 cookie 值,导致后续请求出现 HTTP 302
【发布时间】:2021-03-09 02:06:08
【问题描述】:

从 servlet 容器 HTTP session 迁移到 spring session 成功登录后导致 HTTP 302。登录后我在第一次请求时收到了 HTTP 200,但后续请求似乎再次重定向到登录页面。无法调试后续请求,因为它似乎无法通过我放置断点的 servlet 到达。

现在,我们使用的是 spring session 1.3.5 版本。并且注意到在 spring 的 SessionRepositoryFilter 中,它将原始请求 cookie(例如 servlet 容器)替换为来自 spring session 的值。我不确定这是否是问题的根本原因。如果是,有人可以建议如何解决它吗?还是与某种缺失的配置有关?

这是基于春季会议指南的当前设置:here

Spring 会话 XML 配置:

<context:annotation-config/>
<bean class="org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration"/>
<bean id="hazelcastInstance" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="com.xxx.xxx.xxx.xxx.CustomHazelcastProvider.getInstance"/>
</bean>
<bean class="org.springframework.session.web.http.DefaultCookieSerializer">
    <property name="cookieName" value="JSESSIONID"/>
    <property name="cookiePath" value="/"/>
    <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"/>
</bean>

web.xml中spring XML配置的参考:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:application-context.xml</param-value>
</context-param>

在 web.xml 中注册 spring session 存储库过滤器。如指南中所述,我将其作为过滤器链的第一个条目。

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/rs/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

我已经研究了好几天了,还不知道如何修复它。如果您能提供任何帮助或建议,我们将不胜感激。

提前谢谢你。

【问题讨论】:

  • 请注意,Spring Session 1.3.x 已结束生命周期,这意味着没有进一步的维护版本或安全补丁计划。请尽快迁移到受支持的分支。支持的版本有 2.2.x、2.3.x 和 2.4.x
  • @EleftheriaStein-Kousathana,非常感谢您的评论。由于当前库依赖问题,我们无法升级 2.x.x 版本。更改它还需要更改其他依赖项。我只是将 HTTP 会话策略从 cookie 替换为基于标头。到目前为止,我的应用程序正在按预期工作。

标签: spring session-cookies spring-session


【解决方案1】:

我使用 spring session HeaderHttpSessionStrategy 解决了这个问题。

我的步骤:

  1. 在我的 Spring session XML 配置中,我删除了与 cookie 序列化程序相关的条目以更改 cookie 名称。
<bean class="org.springframework.session.web.http.DefaultCookieSerializer">
   <property name="cookieName" value="JSESSIONID"/>
   <property name="cookiePath" value="/"/>
   <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"/>
</bean>
  1. 默认情况下,spring session 使用CookieHttpSessionStrategy。在 Spring session XML 配置中添加了以下条目。
<bean id="httpSessionStrategy" class="org.springframework.session.web.http.HeaderHttpSessionStrategy"/>
  1. 然后在每个请求上,我都会在 http 请求标头中传递 x-auth-token

上述更改后,应用程序按预期工作。可以毫无问题地登录。

希望此解决方案能帮助遇到相同问题的其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2019-12-15
    • 2020-05-12
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多