Created
June 8, 2020 15:08
-
-
Save cablespaghetti/6f0e8e9d45795a300768b95391172313 to your computer and use it in GitHub Desktop.
spring session configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pulselive.cms.api.common.session; | |
import javax.annotation.PostConstruct; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | |
import org.springframework.session.data.redis.RedisOperationsSessionRepository; | |
import org.springframework.session.data.redis.config.ConfigureRedisAction; | |
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | |
/** | |
* SessionConfiguration class to set up Spring Session for Redis. | |
* | |
* @author Pulse Innovations Ltd. | |
*/ | |
@Configuration | |
@EnableRedisHttpSession | |
@Profile( { "!local" } ) | |
public class SessionConfiguration | |
{ | |
@Value( "${redis.session.host:localhost}" ) | |
private String redisHostname; | |
@Value( "${redis.session.port:6379}" ) | |
private int redisPort; | |
@Value( "${redis.session.password:}" ) | |
private String redisPassword; | |
@Value( "${redis.session.database:0}" ) | |
private int redisDatabase; | |
@Value( "${redis.session.timeout:1800}" ) | |
private int redisTimeout; | |
@Autowired | |
private RedisOperationsSessionRepository sessionRepository; | |
/** | |
* Connection factory for redis. | |
* | |
* @return the jedis connection factory | |
*/ | |
@Bean | |
public JedisConnectionFactory connectionFactory() | |
{ | |
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); | |
jedisConnectionFactory.setHostName( redisHostname ); | |
jedisConnectionFactory.setPort( redisPort ); | |
jedisConnectionFactory.setPassword( redisPassword ); | |
jedisConnectionFactory.setDatabase( redisDatabase ); | |
jedisConnectionFactory.setTimeout( redisTimeout ); | |
return jedisConnectionFactory; | |
} | |
/** | |
* Disable reconfiguration of redis on startup for to enable keyspace events. | |
* | |
* @return the ConfigureRedisAction | |
*/ | |
@Bean | |
public ConfigureRedisAction configureRedisAction() | |
{ | |
return ConfigureRedisAction.NO_OP; | |
} | |
@PostConstruct | |
private void afterPropertiesSet() | |
{ | |
sessionRepository.setDefaultMaxInactiveInterval( redisTimeout ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment