ssm工程引入SpringSession

发现有的项目中使用了SpringSession技术,使用普通Session可以达到多个工程共享Session的目的,感觉很有意义,于是自己在本地搭建了一套demo,来测试一下。
本篇博客借鉴了ssm集成springSession解决session会话丢失,感谢作者分享。
SpringSession的原理其实很简单,就是把HttpSession放入了redis中,所有的seesion操作,操作的都是redis中的这个session,由此达到了session共享的目的。
好了,原理性的东西目前就了解了这么多。下面我们看一下如何在ssm工程中引入SpringSession技术。

1.引入相关依赖

<!-- springsession-->
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
      <version>1.3.0.RELEASE</version>
    </dependency>

2.web.xml配置

配置Spring的filter代理类,将filter的生命周期交予Spring管理。

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

3.Spring配置文件配置

引入SpringSession配置类,我们可以根据项目的需要配置一下SpringSession,比如:我在例子中就配置了SpringSession在redis中的命名空间
另外一个,就是添加了redis的连接工厂类,因为SpringSession的共享是依赖redis的,所以需要引入redis

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" p:redisNamespace="mvc"/>
<bean name="jedisConnectionFactory" 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>

4.使用SpringSession

下面,我们就可以使用SpringSession了,使用SpringSession和使用普通的HttpSession一样,如下例:

@RequestMapping(value = "/get",method = RequestMethod.GET)
    public String getConfigName(HttpServletRequest request,HttpSession session){
        log.info("进入get方法,[{}]",request.getRequestURI());
        session.getAttribute("configName");
        return (String)session.getAttribute("configName");
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值