springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下:
第一步,配置 web.xml
1
2
3
|
< session-config > < session-timeout >15</ session-timeout > </ session-config > |
第二步,配置spring-mvc.xml
1
2
3
4
5
6
7
8
9
10
11
|
<!-- 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 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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
1
2
3
4
5
6
7
8
|
<body> <SCRIPT language= "JavaScript" > alert( "用户已在其他地方登陆,请重新登录。" ); setTimeout( function () { window.top.location.href= "<%=path%>/index.jsp" ; },2000); </script> </body> |
到此 springMvc拦截session失效后处理方式结束。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/fiangasdre/article/details/51802625