如何进行springsession同域下单点登录实现的解析

发布时间:2021-12-17 17:02:14 作者:柒染
来源:亿速云 阅读:103

本篇文章给大家分享的是有关如何进行springsession同域下单点登录实现的解析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Session会话管理

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

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

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

配置文件

pom.xml<dependency>  

<groupId>org.springframework.session</groupId>  

<artifactId>spring-session-data-redis</artifactId>  

<version>1.3.1.RELEASE</version>

</dependency>web.xml  

<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>*.do</url-pattern> 

 </filter-mapping>  

<listener>   

 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>  

<context-param> 

   <param-name>contextConfigLocation</param-name>  

  <param-value>      classpath:applicationContext.xml    </param-value>  

</context-param>applicationContext.xml  <context:annotation-config/> 

 <!-- 初始化一切spring-session准备,且把springSessionFilter放入IOC --> 

 <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">    <property name="maxInactiveIntervalInSeconds" value="300"/>  

</bean>  

<!-- 配置cookie信息-->

  <bean class="org.springframework.session.web.http.DefaultCookieSerializer" id="defaultCookieSerializer">    <property name="cookieName" value="SESSION_NAME"/>   

 <property name="domainName" value="wangjun.com"/>    

<property name="useHttpOnlyCookie" value="true"/>  

  <property name="cookiePath" value="/"/>   

 <property name="cookieMaxAge" value="31536000"/> 

 </bean>  

<!-- 配置redis连接池信息-->  

  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  

  <property name="maxTotal" value="20"/> 

 </bean>  

<!--配置redis连接信息--> 

 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">   

 <property name="hostName" value="127.0.0.1"/>  

  <property name="port" value="6379"/>    

<property name="poolConfig" ref="jedisPoolConfig"/> 

 </bean>

代码测试

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);  }}

以上就是如何进行springsession同域下单点登录实现的解析,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. 域的同林迁移
  2. SpringSession+Redis实现集群会话共享的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springsession

上一篇:Python并行和并发的区别是什么

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》