场景:

        现在使用的框架中有整合redis,用来缓存登录信息和保存在session中的一些信息,以前没有特别关注,最近有点时间,自己动手整合了下,记录下整合过程,以备以后不时之需。


第一步:添加涉及的jar包

使用maven 在pom.xml中添加引入

<!--redis-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session</artifactId>
      <version>1.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.1</version>
    </dependency>

第二步:编写 redis 的相关连接配置信息

redis.hostName=192.168.1.205
redis.port=6379
redis.password=

redis还有很多的配置,我这边图方便,就只写了连接信息。


这里可以单独创建一个 redis.properties 文件,也可以把 redis 的连接配置信息写到其他的配置文件中,只要后面在 Spring 配置文件中有引入该配置就好

第三步:在 Spring 配置文件中增加redis的配置

首先,记得把写有redis的连接信息的配置文件引入进来

<!-- 引入配置文件 -->
    <context:property-placeholder location="classpath:redis.properties" />  


    <!-- Spring-redis连接池管理工厂 -->
    <bean
            class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostName}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
    </bean>

    <!-- 将session放入redis -->
    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

    <!-- redis缓存配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="300" />
        <property name="maxTotal" value="60000" />
        <property name="testOnBorrow" value="true" />
    </bean>
    <!--连接池-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="${redis.hostName}"  type="java.lang.String"/>
        <constructor-arg index="2" value="${redis.port}" type="int" />
    </bean>

第四步:更改 web.xml ,增加拦截器

<!-- spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

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

这里有个注意点:

添加redis连接信息的spring配置文件,要在web.xml 文件中配置过,启动web项目时,容器会去读它的配置文件web.xml 中读两个节点<context-param>和<listenter>,容器创建一个ServletContext(上下文),这个web项目所有部分都将共享这个上下文,容器将<context-param></context-param>转化为键值对,并交给ServletContext。

spring session延期后redis延期在哪修改 spring-session redis_spring



至此,Spring 整合 redis 就已经完成了,可以重启项目验证了。

在 demo 工程中,前台传入登录信息,因为只是简单的demo,没有做密码加密和登录校验,在登录方法中将登录信息存入session中,并在控制台打印相应的 session 信息。

spring session延期后redis延期在哪修改 spring-session redis_配置文件_02


在 redis 中根据sessionId 查看保存的session信息,在这里,我使用的是 RedisDesktopManager   redis 可视化工具,有需要可以百度下载按照和使用。

根据上面demo工程控制台打印信息,我们可以知道 ,当前sessionId 为 6731e78e-4ad2-43e3-9ef8-d5b0725d2ead ,而保存在redis中的key则为  spring:session:sessions:6731e78e-4ad2-43e3-9ef8-d5b0725d2ead,这个格式是spring生成的,还没去看是否可以更改,但正常不需要更改也可以使用。

spring session延期后redis延期在哪修改 spring-session redis_redis_03

如上图所示,当前redis中已经保存了key值为 spring:session:sessions:6731e78e-4ad2-43e3-9ef8-d5b0725d2ead 的数据,因为保存进redis中的数据都是经过序列化的,所以在当前可视化工具中看到的是乱码,我们用redis命令 

HGET   spring:session:sessions:6731e78e-4ad2-43e3-9ef8-d5b0725d2ead sessionAttr:account 查看

spring session延期后redis延期在哪修改 spring-session redis_spring redis_04

由此可以看到当前里面存的是 account ,和我们之前控制台打印的账号信息一致。

\xAC\xED\x00\x05t\x00\x07  这个是redis 序列化时生成的。