spring session同域下单点登录实现解析

网友投稿 412 2023-07-16

spring session同域下单点登录实现解析

spring session同域下单点登录实现解析

Session会话管理

在Web项目开发中,Session会话管理是一个很重要的部分,用于存储与记录用户的状态或相关的数据;通常情况下session交由容器(tomcat)来负责存储和管理,但是如果项目部署在多台tomcat中,则session管理存在很大的问题;

1、多台tomcat之间无法共享session,比如用户在tomcat A服务器上已经登录了,但当负载均衡跳转到tomcat B时,由于tomcat B服务器并没有用户的登录信息,session就失效了,用户就退出了登录;

2、一旦tomcat容器关闭或重启也会导致session会话失效;因此如果项目部署在多台tomcat中,就需要解决session共享的问题;

配置文件

pom.xml

org.springframework.session

spring-session-data-redis

1.3.1.RELEASE

web.xml

  

springSessionRepositoryFilter

org.springframework.web.filter.DelegatingFilterProxy

springSessionRepositoryFilter

*.do

  

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

applicationContext.xml

  

    

  

代码测试

public class SessionServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doGet(request,response);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String sesssionID = request.getSession().getId();

//部署两份,把这个地方8081改成8080就行了,只是为了区分

response.getWriter().write("8081 Server SessionID"+sesssionID);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot向elk写日志实现过程
下一篇:idea2019版与maven3.6.2版本不兼容的解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~