From 417174388bb33158575bd4fadc4a73e5cb3e1049 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Tue, 30 Sep 2014 13:40:15 -0500 Subject: [PATCH] Add EnableRedisHttpSession Fixes gh-42 --- README.adoc | 22 +------ samples/web/src/main/java/Config.java | 38 +++--------- samples/web/src/main/java/Initializer.java | 24 ++++---- spring-session/build.gradle | 3 +- .../web/http/EnableRedisHttpSession.java | 55 +++++++++++++++++ .../http/RedisHttpSessionConfiguration.java | 59 +++++++++++++++++++ 6 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java create mode 100644 spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java diff --git a/README.adoc b/README.adoc index 9c350046..c9ffc35b 100644 --- a/README.adoc +++ b/README.adoc @@ -164,31 +164,13 @@ Add the following Spring Configuration: [source,java] ---- @Configuration +@EnableRedisHttpSession public class Config { @Bean public JedisConnectionFactory connectionFactory() throws Exception { return new JedisConnectionFactory(); } - - @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { - RedisTemplate template = new RedisTemplate(); - template.setKeySerializer(new StringRedisSerializer()); - template.setHashKeySerializer(new StringRedisSerializer()); - template.setConnectionFactory(connectionFactory); - return template; - } - - @Bean - public RedisOperationsSessionRepository sessionRepository(RedisTemplate redisTemplate) { - return new RedisOperationsSessionRepository(redisTemplate); - } - - @Bean - public SessionRepositoryFilter sessionFilter(SessionRepository sessionRepository) { - return new SessionRepositoryFilter(sessionRepository); - } } ---- @@ -208,7 +190,7 @@ public class Initializer extends AbstractContextLoaderInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); - servletContext.addFilter("sessionFilter", DelegatingFilterProxy.class) + servletContext.addFilter("springSessionFilter", DelegatingFilterProxy.class) .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); } diff --git a/samples/web/src/main/java/Config.java b/samples/web/src/main/java/Config.java index 054384f7..d813b05f 100644 --- a/samples/web/src/main/java/Config.java +++ b/samples/web/src/main/java/Config.java @@ -18,14 +18,8 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.StringRedisSerializer; -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.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import redis.clients.jedis.Protocol; import redis.embedded.RedisServer; @@ -34,8 +28,14 @@ import redis.embedded.RedisServer; * @author Rob Winch */ @Configuration +@EnableRedisHttpSession public class Config { + @Bean + public JedisConnectionFactory connectionFactory() throws Exception { + return new JedisConnectionFactory(); + } + @Bean public RedisServerBean redisServer() { return new RedisServerBean(); @@ -58,28 +58,4 @@ public class Config { } } } - - @Bean - public JedisConnectionFactory connectionFactory() throws Exception { - return new JedisConnectionFactory(); - } - - @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { - RedisTemplate template = new RedisTemplate(); - template.setKeySerializer(new StringRedisSerializer()); - template.setHashKeySerializer(new StringRedisSerializer()); - template.setConnectionFactory(connectionFactory); - return template; - } - - @Bean - public RedisOperationsSessionRepository sessionRepository(RedisTemplate redisTemplate) { - return new RedisOperationsSessionRepository(redisTemplate); - } - - @Bean - public SessionRepositoryFilter sessionFilter(SessionRepository sessionRepository) { - return new SessionRepositoryFilter(sessionRepository); - } } diff --git a/samples/web/src/main/java/Initializer.java b/samples/web/src/main/java/Initializer.java index b9fe355a..d47b4710 100644 --- a/samples/web/src/main/java/Initializer.java +++ b/samples/web/src/main/java/Initializer.java @@ -29,17 +29,17 @@ import java.util.EnumSet; * @author Rob Winch */ public class Initializer extends AbstractContextLoaderInitializer { - @Override - public void onStartup(ServletContext servletContext) throws ServletException { - super.onStartup(servletContext); - servletContext.addFilter("sessionFilter", DelegatingFilterProxy.class) - .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); - } + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + super.onStartup(servletContext); + servletContext.addFilter("springSessionRepositoryFilter", DelegatingFilterProxy.class) + .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); + } - @Override - protected WebApplicationContext createRootApplicationContext() { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(Config.class); - return context; - } + @Override + protected WebApplicationContext createRootApplicationContext() { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(Config.class); + return context; + } } diff --git a/spring-session/build.gradle b/spring-session/build.gradle index 192e24ea..96dc719e 100644 --- a/spring-session/build.gradle +++ b/spring-session/build.gradle @@ -10,7 +10,8 @@ configurations { } dependencies { - optional "org.springframework.data:spring-data-redis:$springDataRedisVersion" + optional "org.springframework.data:spring-data-redis:$springDataRedisVersion", + "org.springframework:spring-context:$springVersion" provided "javax.servlet:javax.servlet-api:$servletApiVersion" integrationTestCompile "redis.clients:jedis:2.4.1", "org.apache.commons:commons-pool2:2.2", 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 new file mode 100644 index 00000000..230a9083 --- /dev/null +++ b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.session.data.redis.config.annotation.web.http; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.redis.connection.RedisConnectionFactory; + +/** + * Add this annotation to an {@code @Configuration} class to expose the + * SessionRepositoryFilter as a bean named "springSessionRepositoryFilter" and + * backed by Redis. In order to leverage the annotation, a single {@link RedisConnectionFactory} + * must be provided. For example: + * + * + * @Configuration + * @EnableRedisHttpSession + * public class RedisHttpSessionConfig { + * + * @Bean + * public JedisConnectionFactory connectionFactory() throws Exception { + * return new JedisConnectionFactory(); + * } + * + * } + * + * + * More advanced configurations can extend {@link RedisHttpSessionConfiguration} instead. + * + * @author Rob Winch + * @since 1.0 + */ +@Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME) +@Target(value={java.lang.annotation.ElementType.TYPE}) +@Documented +@Import(RedisHttpSessionConfiguration.class) +@Configuration +public @interface EnableRedisHttpSession {} \ 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 new file mode 100644 index 00000000..052daeb4 --- /dev/null +++ b/spring-session/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.session.data.redis.config.annotation.web.http; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.session.ExpiringSession; +import org.springframework.session.SessionRepository; +import org.springframework.session.data.redis.RedisOperationsSessionRepository; +import org.springframework.session.web.http.SessionRepositoryFilter; + +/** + * Exposes the {@link SessionRepositoryFilter} as a bean named + * "springSessionRepositoryFilter". In order to use this a single + * {@link RedisConnectionFactory} must be exposed as a Bean. + * + * @author Rob Winch + * @since 1.0 + * + * @see EnableRedisHttpSession + */ +@Configuration +public class RedisHttpSessionConfiguration { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate(); + template.setKeySerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); + template.setConnectionFactory(connectionFactory); + return template; + } + + @Bean + public RedisOperationsSessionRepository sessionRepository(RedisTemplate redisTemplate) { + return new RedisOperationsSessionRepository(redisTemplate); + } + + @Bean + public SessionRepositoryFilter springSessionRepositoryFilter(SessionRepository sessionRepository) { + return new SessionRepositoryFilter(sessionRepository); + } +}