欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解

 更新時間:2017年11月11日 10:10:05   作者:渡劫錦官城  
這篇文章主要介紹了SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。

本文主要向大家介紹了SpringMVC攔截器實(shí)現(xiàn):當(dāng)用戶訪問網(wǎng)站資源時,監(jiān)聽session是否過期的代碼,具體如下:

一、攔截器配置

<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/user/login"/>  <!-- 不攔截登錄請求 -->
    <mvc:exclude-mapping path="/user/logout"/>  <!-- 不攔截注銷請求 -->
    <mvc:exclude-mapping path="*.jsp"/>
    <mvc:exclude-mapping path="*.html"/>
    <mvc:exclude-mapping path="*.js"/>
    <mvc:exclude-mapping path="*.css"/>
    <bean class="org.huaxin.interceptor.AccessInterceptor"></bean>
  </mvc:interceptor>
</mvc:interceptors>

二、攔截器編碼

public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
      Object obj) throws Exception {
    System.out.println("[AccessInterceptor]:preHandle執(zhí)行");
    HttpSession session = request.getSession();
    ServletContext application = session.getServletContext();
    if(application.getAttribute(session.getId()) == null){  //未登錄
      PrintWriter out = response.getWriter();
      StringBuffer sb = new StringBuffer("<script type=\"text/javascript\" charset=\"UTF-8\">");
      sb.append("alert(\"你的賬號被擠掉,或者沒有登錄,或者頁面已經(jīng)過期,請重新登錄\")");
      sb.append("window.location.href='/user/logout';");
      sb.append("</script>");
      out.print(sb.toString());
      out.close();
      return false;
    }else{  //已經(jīng)登錄
      return true;
    }
  }

三、總結(jié)

1.注意這里使用的攔截器是HandlerInterceptor,你的攔截器需要實(shí)現(xiàn)這個接口

2.在你的登錄handler里面,要將session保存到application中,方便根據(jù)sessionId來判斷是否存在session

3.sb.append("window.location.href='/user/logout';"); 這行代碼是說,執(zhí)行注銷操作,在你的/user/logout 這個handler里面得把頁面解析到登錄頁,方便重新登錄

以上就是本文關(guān)于SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解的全部內(nèi)容,希望對大家有所幫助,感興趣的朋友可以繼續(xù)參閱本站:Java監(jiān)聽器的作用及用法代碼示例、SpringMVC開發(fā)restful API之用戶查詢代碼詳解、springmvc接收jquery提交的數(shù)組數(shù)據(jù)代碼分享等,如有不足之處,歡迎留言指出。小編會及時進(jìn)行更改,感謝朋友們對本站的支持!

相關(guān)文章

最新評論