spring session 配置

2022-03-28 19:03:20

一、下载最新版的redis


spring session要求redis版本必须2.8以上,并且需要配置redis.windows.conf 中notify-keyspace-events "" , 不然会报Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events 异常


windows版本下载地址:


https://github.com/MicrosoftArchive/redis/releases


二、配置maven中pom.xml 文件引入所需依赖包


<!-- redis -->

<dependency>

  <groupId>org.springframework.session</groupId>

  <artifactId>spring-session-data-redis</artifactId>

  <version>1.2.1.RELEASE</version>

</dependency>

<dependency>

  <groupId>redis.clients</groupId>

  <artifactId>jedis</artifactId>

  <version>2.8.1</version>

</dependency>

三、applicationContext.xml配置


<!-- redis session config -->

<bean id="redisHttpSessionConfiguration"

      class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

    <property name="maxInactiveIntervalInSeconds" value="600"/>

</bean>

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

    <property name="maxTotal" value="${redis.maxTotal}" />

    <property name="maxIdle" value="${redis.maxIdle}" />

</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_pwd}" /> -->

    <property name="timeout" value="${redis.timeout}"/>

    <property name="usePool" value="true"/>

    <property name="poolConfig" ref="jedisPoolConfig"/>

</bean>

三、配置web.xml文件


<!-- spring session 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>

</filter-mapping>