spring会话更改了cookie值,导致在后续请求中出现http302

lyfkaqu1  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(361)

从servlet容器http会话迁移到spring会话在成功登录后导致了http302。我在登录后的第一个请求中得到了HTTP200,但随后的请求似乎又被重定向到了登录页面。无法对后续请求进行调试,因为它似乎无法通过我放置了一些断点的servlet访问。
现在,我们使用的是SpringSession1.3.5版本。我们注意到,在spring的sessionrepositoryfilter中,它将原始的请求cookie(例如servlet容器)替换为spring会话的值。我不确定这是否是问题的根源。如果是,有人能建议如何解决吗?还是与某种配置缺失有关?
以下是基于spring会话指南的当前设置: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会话存储库筛选器。如指南中所述,我将其作为过滤器链的第一个条目。

<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>

我已经做了好几天了,还不知道怎么修。非常感谢您的帮助或建议。
先谢谢你。

ubbxdtey

ubbxdtey1#

我用spring会话解决了这个问题 HeaderHttpSessionStrategy .
我的步骤是:
在spring会话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>

默认情况下,spring会话使用 CookieHttpSessionStrategy . 在spring会话xml配置中添加了以下条目。

<bean id="httpSessionStrategy" class="org.springframework.session.web.http.HeaderHttpSessionStrategy"/>

然后在每个请求中,我都在http请求头中传递x-auth-token。
在上述更改之后,应用程序按预期工作。能够登录没有问题。
希望这个解决方案能帮助其他遇到同样问题的人。

相关问题