我正在考虑将用户会话从应用程序级别移动到Redis实例。我相信我已根据文档(http://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession)正确设置了所有内容,但我没有看到我期待的行为,并认为我在某个地方错过了一步。
应用程序当前使用HttpSession,因此我只是将以下内容添加到上下文中:
<context:annotation-config/>
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
<beans:bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<beans:bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="HOSTNAME" p:port="6379" />
在web.xml中添加了以下内容:
<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>/*</url-pattern>
</filter-mapping>
应用程序构建,部署和加载页面很好,但是当我查看页面上的cookie时,我同时拥有JSESSIONID和SESSION。我知道JSESSIONID是由Spring Security使用的,看起来Spring Session使用了SESSION。当我查看redis时,看起来SESSION就是存储的那个。
另一个问题是自定义会话对象(使用session.setAttribute添加)未显示在会话中。会话中显示的唯一内容是AFTER登录后,是SPRING_SECURITY_CONTEXT对象。当我删除Spring Session过滤器时,这些对象就被添加到会话中了。
这是正常行为,还是由于我的设置而导致一些奇怪的冲突?
答案 0 :(得分:3)
我遇到了同样的问题,最后发现这是我的web.xml中声明的过滤器顺序错误的结果。请求通过的第一个过滤器是spring安全过滤器,它在响应中设置了JSESSIONID cookie,然后Spring会话存储库过滤器开始设置它自己的SESSION cookie。更改顺序以便Spring会话存储库过滤器首先执行其操作后,一切正常。
答案 1 :(得分:1)
有同样的问题。但过滤器排序没有帮助。
当JSESSIONID在请求中传递时,它可以是SESSION和JSESSIONID,然后spring会话将SESSION cookie与提供的cookie一起添加(您可以在请求中清理cookie以仅接收SESSION)。
在我的情况下发生这种情况的原因是:
默认情况下,cookie由应用程序服务器(tomcat)提供,当我添加spring会话时,cookie将由spring提供。 tomcat的默认值是JSESSIONID,spring(DefaultCookieSerializer.class)的默认值是SESSION
要解决此问题,我只需在WEB.xml中指定cookie名称:
<session-config>
<cookie-config>
<name>JSESSIONID</name>
</cookie-config>
</session-config>
这将为tomcat和spring session filter
指定cookie名称答案 2 :(得分:1)
我有同样的问题。问题是tomcat,它将添加JSESSIONID cookie作为响应。因此,我添加了CookieSerializer
并解决了问题。
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
也不要忘记像这样在tomcat中更改context.xml
:
<Context cookies="false">
....
</Context>