包依赖
<!-- redis start -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<!-- 这个是redis的来凝结器可以选择自己喜欢的 -->
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>3.5.0.Final</version>
</dependency>
XML配置
- spring.xml
<!-- redis start -->
<cache:annotation-driven/>
<!-- redis连接工厂 -->
<bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="lettuceConnectionFactory" />
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean> >
<!-- 把配置类加载到spring容器 **重要**-->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<!-- 过期时间100分钟 -->
<!-- 配置RedisSession -->
<property name="maxInactiveIntervalInSeconds" value="6000"></property>
</bean>
<!-- redis end -->
- web.xml
<!-- 添加过滤器,这个过滤器会自动包装session,request,response -->
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
java-config方式
这是在redis连接已经配置好的情况下
- 创建一个类来启用EnableRedisHttpSession
//这里用@Configuration注解来启用配置类,你也就可以用其他方式
@Configuration
@EnableRedisHttpSession
public class Config {
}
- 创建一个初始化类继承AbstractHttpSessionApplicationInitializer
//这个类会在初始化的时候插入filter
public class Initializer extends AbstractHttpSessionApplicationInitializer {
/*
你也可以把刚刚的配置类放在这里引入
public Initializer() {
super(Config.class);
}
*/
}
转载于:https://www.cnblogs.com/A-yes/p/9894164.html