1. 程式人生 > >使用spring-session及redis做分散式session

使用spring-session及redis做分散式session

使用spring-session需要引入以下jar包

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        
<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.2.RELASE</version>
        </dependency>

 
<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.5.2.RELASE</version>
        </dependency>

修改web.xml檔案

新增spring-session過濾器代理,需放在其餘過濾器之前

 <!--分散式session-->
    <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>

    <filter>
        <filter-name>xssFilter</filter-name>
        <filter-class>cn.nsu.edu.web.four.filters.XssFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>xssFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

編輯spring配置檔案,spring容器或者mvc容器均可

    <!--這個類將會自動注入過濾器,防止出現找不到過濾器的錯-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
    </bean>

配置redis連線池以及spring-session工廠類,序列化方式採用spring-session預設的jdk序列方法,即所有bean物件需要實現Serializerable介面

<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxActive}"/>
        <!--最大空閒連線數 -->
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <!--初始化連線數 -->
        <property name="minIdle" value="${redis.pool.minIdle}"/>
        <!--最大等待時間 -->
        <property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
        <!--對拿到的connection進行validateObject校驗 -->
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <!--在進行returnObject對返回的connection進行validateObject校驗 -->
        <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
        <!--定時對執行緒池中空閒的連結進行validateObject校驗 -->
        <property name="testWhileIdle" value="false"/>
    </bean>


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

    </bean>


    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>

啟動專案,即可使用redis取代容器的session。

spring-session自定義序列化方法