欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring session redis 配置

程序员文章站 2022-06-16 08:04:46
...

 

一、引入依赖

pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.3.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

 

二、spring配置

spring-session.xml (名字任意)

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
	<!-- 设置Session失效时间 -->
	<property name="maxInactiveIntervalInSeconds" value="1800"></property>
</bean>
       
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxTotal" value="${redis.jedisPoolConfig.maxTotal}" />
	<property name="maxIdle" value="${redis.jedisPoolConfig.maxIdle}" />
	<property name="maxWaitMillis" value="${redis.jedisPoolConfig.maxWaitMillis}" />
	<property name="testOnBorrow" value="${redis.jedisPoolConfig.testOnBorrow}" />
	<property name="testOnReturn" value="${redis.jedisPoolConfig.testOnReturn}" />
</bean>

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
	<property name="hostName" value="${redis.hostName}"/>
	<property name="port" value="${redis.port}"/>
	<property name="password" value="${redis.password}" />
	<property name="database" value="10" />
	<property name="timeout" value="60000"/>
	<property name="usePool" value="true"/>
	<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

 

redis.properties

redis.hostName=localhost
redis.password=password
redis.port=6379

redis.jedisPoolConfig.maxTotal=200
redis.jedisPoolConfig.maxIdle=50
redis.jedisPoolConfig.maxWaitMillis=60000
redis.jedisPoolConfig.testOnBorrow=true
redis.jedisPoolConfig.testOnReturn=true

 

三、配置filter

web.xml,(要引入spring-session.xml)

第一个filter

<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>