RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
在Spring-Session使用Redis如何实现共享session

这期内容当中小编将会给大家带来有关在Spring-Session使用redis如何实现共享session,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

成都创新互联专注于海湖新企业网站建设,成都响应式网站建设,电子商务商城网站建设。海湖新网站建设公司,为海湖新等地区提供建站服务。全流程定制网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

1、添加依赖


 org.springframework.session
 spring-session-data-redis
 1.2.1.RELEASE


 redis.clients
 jedis
 2.8.1

2、配置

spring-mvc.xml:


  



  
  



  
  
  
  
  
  

web.xml添加拦截器:


  springSessionRepositoryFilter
  org.springframework.web.filter.DelegatingFilterProxy


  springSessionRepositoryFilter
  /*

3、使用spring-session

只要使用标准的servlet api调用session,在底层就会通过Spring Session得到的,并且会存储到Redis或其他你所选择的数据源中。

这里是我写的一个demo:

/**
 * @author fengzp
 * @date 17/2/23下午3:19
 */
@Controller
@RequestMapping(value = "index")
public class IndexController {

  private final Gson gson = new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create();

  @RequestMapping(value = "login")
  public String login(HttpServletRequest request, String username){

    request.getSession().setAttribute("user", gson.toJson(new User(username,"123456")));

    return "login";
  }

  @RequestMapping(value = "index")
  public String index(HttpServletRequest request, Model model){

    User user = gson.fromJson(request.getSession().getAttribute("user").toString(), User.class);

    model.addAttribute("user", user);

    return "index";
  }
}

index.jsp:

第一个tomcat



Hello World!

${user.username}

第二个tomcat



Hello World! i am the second!

${user.username}

测试

这里利用上一篇nginx负载配置的两个tomcat来测试。

首先访问 http://192.168.99.100/feng/index/login.htm?username=nginx 来触发生成session。

查看redis,发现session已经保存到redis。

在Spring-Session使用Redis如何实现共享session

访问 http://192.168.99.100/feng/index/index.htm 来读取session, 并刷新多次。

在Spring-Session使用Redis如何实现共享session

在Spring-Session使用Redis如何实现共享session

上述就是小编为大家分享的在Spring-Session使用Redis如何实现共享session了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


本文名称:在Spring-Session使用Redis如何实现共享session
链接分享:http://sczitong.cn/article/ihgppp.html