问题
We have two projects behind same domain ( zuul proxy in front of them ), both uses spring session project with sessions kept in redis.
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?
回答1:
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" name is a default set in source of CookieHttpSessionStrategy.
回答2:
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("YOUR_COOKIE");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
回答3:
I found a blog post about this, spring-session学习
This post explains how to change session id name with Spring XML.
like following:
<bean class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="SYSTEM_SESSION_ID" />
</bean>
And, I actually tested it and it worked.
回答4:
I know that this is an old question, but I just want to put that this option also works.
You can add server.servlet.session.cookie.name
in your application.yml. Take a look at this spring docs link, it has other cookie properties that you can change as well.
Spring Common Application Properties
来源:https://stackoverflow.com/questions/33095345/how-to-change-spring-session-redis-cookie-name