From 6a22a11187848555b17ce1caa3e3f8d8c93bd7e0 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Wed, 1 Oct 2014 16:14:40 -0500 Subject: [PATCH] Allow configure expires for EnableRedisHttpSession --- .../web/http/EnableRedisHttpSession.java | 4 +- .../http/RedisHttpSessionConfiguration.java | 49 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java index 230a908..2436671 100644 --- a/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java +++ b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java @@ -52,4 +52,6 @@ import org.springframework.data.redis.connection.RedisConnectionFactory; @Documented @Import(RedisHttpSessionConfiguration.class) @Configuration -public @interface EnableRedisHttpSession {} \ No newline at end of file +public @interface EnableRedisHttpSession { + int maxInactiveIntervalInSeconds() default 1800; +} \ No newline at end of file diff --git a/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java index 052daeb..5aeb1d4 100644 --- a/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java +++ b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java @@ -15,8 +15,15 @@ */ package org.springframework.session.data.redis.config.annotation.web.http; +import java.util.Map; + +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportAware; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -24,6 +31,7 @@ import org.springframework.session.ExpiringSession; import org.springframework.session.SessionRepository; import org.springframework.session.data.redis.RedisOperationsSessionRepository; import org.springframework.session.web.http.SessionRepositoryFilter; +import org.springframework.util.ClassUtils; /** * Exposes the {@link SessionRepositoryFilter} as a bean named @@ -36,7 +44,12 @@ import org.springframework.session.web.http.SessionRepositoryFilter; * @see EnableRedisHttpSession */ @Configuration -public class RedisHttpSessionConfiguration { +public class RedisHttpSessionConfiguration implements ImportAware, BeanClassLoaderAware { + + private ClassLoader beanClassLoader; + + private Integer maxInactiveIntervalInSeconds; + @Bean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { @@ -49,11 +62,43 @@ public class RedisHttpSessionConfiguration { @Bean public RedisOperationsSessionRepository sessionRepository(RedisTemplate redisTemplate) { - return new RedisOperationsSessionRepository(redisTemplate); + RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(redisTemplate); + sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds); + return sessionRepository; } @Bean public SessionRepositoryFilter springSessionRepositoryFilter(SessionRepository sessionRepository) { return new SessionRepositoryFilter(sessionRepository); } + + @Override + public void setImportMetadata(AnnotationMetadata importMetadata) { + + Map enableAttrMap = importMetadata.getAnnotationAttributes(EnableRedisHttpSession.class.getName()); + AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap); + if(enableAttrs == null) { + // search parent classes + Class currentClass = ClassUtils.resolveClassName(importMetadata.getClassName(), beanClassLoader); + for(Class classToInspect = currentClass ;classToInspect != null; classToInspect = classToInspect.getSuperclass()) { + EnableRedisHttpSession enableWebSecurityAnnotation = AnnotationUtils.findAnnotation(classToInspect, EnableRedisHttpSession.class); + if(enableWebSecurityAnnotation == null) { + continue; + } + enableAttrMap = AnnotationUtils + .getAnnotationAttributes(enableWebSecurityAnnotation); + enableAttrs = AnnotationAttributes.fromMap(enableAttrMap); + } + } + maxInactiveIntervalInSeconds = enableAttrs.getNumber("maxInactiveIntervalInSeconds"); + } + + + + /* (non-Javadoc) + * @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader) + */ + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } }