0

I'm using spring-session + redis as documented here: http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-xml.html

How can I configure RedisHttpSessionConfigure such that for local development, redis is not needed and the application will simply default to the container session handling?

user2395365
  • 1,991
  • 4
  • 18
  • 34

1 Answers1

1

Generally this isn't recommended because you are differing your development environment from your production environment. It should be quite trivial to point your dev machine to a Redis instance.

If you need to support it, you can use Spring profiles. For example, with XML you can use something like:

<beans profile="dev">
    <bean id="springSessionRepositoryFilter" class="org.springframework.web.filter.CharacterEncodingFilter"/>
</beans>

<beans profile="production">
    <context:annotation-config/>
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>
</beans>

The key is to ensure that your dev environment also has a Bean that implements Filter named springSessionRepositoryFilter. In this example, I used CharacterEncodingFilter which should do nothing since the encoding property is not set but feel free to replace with whatever you like.

The next thing you will need to do is activate your environments. For example, you can use

-Dspring.profiles.active="production"
Rob Winch
  • 21,440
  • 2
  • 59
  • 76
  • I'm looking to configure a backup session store in production if redis goes offline for some reason. How can I do that in spring boot? I'm running 3 instances of service in docker and using redis as session store. I do not want application to fail and use guavava or container default session store if redis goes down for some reason or if not available during start up. – Vineet Dec 03 '22 at 11:37