詳解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數(shù)據(jù)庫連接PreparedStatement的使用詳解
這篇文章主要介紹了Java數(shù)據(jù)庫連接PreparedStatement的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
詳解mybatis.generator配上最新的mysql 8.0.11的一些坑
這篇文章主要介紹了詳解mybatis.generator配上最新的mysql 8.0.11的一些坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
Spring Cloud超詳細(xì)i講解Feign自定義配置與使用
這篇文章主要介紹了SpringCloud Feign自定義配置與使用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作
這篇文章主要介紹了MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

