欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在spring中使用session實現同域下單點登錄

本篇文章給大家分享的是有關怎么在spring中使用session實現同域下單點登錄,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

我們提供的服務有:成都網站設計、網站制作、微信公眾號開發(fā)、網站優(yōu)化、網站認證、清豐ssl等。為1000+企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的清豐網站制作公司

Session會話管理

在Web項目開發(fā)中,Session會話管理是一個很重要的部分,用于存儲與記錄用戶的狀態(tài)或相關的數據;通常情況下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就行了,只是為了區(qū)分
    response.getWriter().write("8081 Server SessionID"+sesssionID);
  }
}

以上就是怎么在spring中使用session實現同域下單點登錄,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯行業(yè)資訊頻道。

網頁題目:怎么在spring中使用session實現同域下單點登錄
網頁網址:http://www.chinadenli.net/article40/gicgho.html

成都網站建設公司_創(chuàng)新互聯,為您提供企業(yè)網站制作企業(yè)建站響應式網站關鍵詞優(yōu)化靜態(tài)網站軟件開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

h5響應式網站建設