詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面
springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面,廢話不多少了,具體如下:
第一步,配置 web.xml
<session-config> <session-timeout>15</session-timeout> </session-config>
第二步,配置spring-mvc.xml
<!-- Session失效攔截 --> <mvc:interceptors> <!-- 定義攔截器 --> <mvc:interceptor> <!-- 匹配的是url路徑, 如果不配置或/**,將攔截所有的Controller --> <mvc:mapping path="/**" /> <!-- 不需要攔截的地址 --> <mvc:exclude-mapping path="/login.do" /> <bean class="com.cm.contract.controller.annotation.GEISSSessionTimeoutInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
第三步,寫攔截器SystemSessionInterceptor 方法
public class SystemSessionInterceptor implements HandlerInterceptor { private static final String LOGIN_URL="/jsp/sessionrun.jsp"; @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session=request.getSession(true); //session中獲取用戶名信息 Object obj = session.getAttribute(CMConstant.LOGINUSER); if (obj==null||"".equals(obj.toString())) { response.sendRedirect(request.getSession().getServletContext().getContextPath()+LOGIN_URL; return false; } return true; }
第五步,配置友情提示頁面sessionrun.jsp
<body> <SCRIPT language="JavaScript"> alert("用戶已在其他地方登陸,請重新登錄。"); setTimeout(function () { window.top.location.href="<%=path%>/index.jsp"; },2000); </script> </body>
到此 springMvc攔截session失效后處理方式結(jié)束。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)系列之CountDownLatch源碼分析
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之CountDownLatch源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Intellij?IDEA?中調(diào)試?maven?插件的步驟
這篇文章主要介紹了Intellij?IDEA?中調(diào)試?maven?插件,本文分步驟給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03SpringBoot使用AOP與注解實現(xiàn)請求參數(shù)自動填充流程詳解
面向切面編程(aspect-oriented programming,AOP)主要實現(xiàn)的目的是針對業(yè)務(wù)處理過程中的切面進(jìn)行提取,諸如日志、事務(wù)管理和安全這樣的系統(tǒng)服務(wù),從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率2023-02-02