如何更改 spring 会话(redis)cookie 名称? [英] How to change spring session (redis) cookie name?

查看:58
本文介绍了如何更改 spring 会话(redis)cookie 名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在同一个域后面有两个项目(前面是zuul代理),都使用spring session项目,会话保存在redis中.

We have two projects behind same domain ( zuul proxy in front of them ), both uses spring session project with sessions kept in redis.

这两个会话应该不同,但似乎它们在名为SESSION"的 cookie 中覆盖了彼此的 ID.这个名字怎么改?有没有什么简单的方法可以通过配置来做到这一点?

Those two sessions should be different, but seems they are overwriting each other id in cookie named 'SESSION'. How to change that name? Is there any easy way to do that through configuration?

推荐答案

好的,我没有在配置中找到任何属性来改变它.我在 spring-session 源代码中挖掘了一点,最后做:

ok, I did not find any property in configuration to change that. I dig in a bit in spring-session source code, and finally do:

@Bean
public <S extends ExpiringSession> SessionRepositoryFilter<? extends ExpiringSession> springSessionRepositoryFilter(SessionRepository<S> sessionRepository, ServletContext servletContext) {
    SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<S>(sessionRepository);
    sessionRepositoryFilter.setServletContext(servletContext);
    CookieHttpSessionStrategy httpSessionStrategy = new CookieHttpSessionStrategy();
    httpSessionStrategy.setCookieName("MY_SESSION_NAME");
    sessionRepositoryFilter.setHttpSessionStrategy(httpSessionStrategy);
    return sessionRepositoryFilter;
}

SESSION"名称是 CookieHttpSessionStrategy 源中的默认设置.

"SESSION" name is a default set in source of CookieHttpSessionStrategy.

这篇关于如何更改 spring 会话(redis)cookie 名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆