springmvc controls users to jump to the login page when session fails
- 2020-05-27 05:34:10
- OfStack
springmvc controls the login user session to jump to the login page after the failure of session. The details are as follows:
Step 1, configure web.xml
<session-config>
<session-timeout>15</session-timeout>
</session-config>
Step 2, configure spring-mvc.xml
<!-- Session The failure to intercept -->
<mvc:interceptors>
<!-- Define interceptors -->
<mvc:interceptor>
<!-- Matching is url Path, If not configured or /**, Will intercept all Controller -->
<mvc:mapping path="/**" />
<!-- Addresses that do not need to be intercepted -->
<mvc:exclude-mapping path="/login.do" />
<bean class="com.cm.contract.controller.annotation.GEISSSessionTimeoutInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
Step 3, write the interceptor SystemSessionInterceptor method
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 Get the user name information in
Object obj = session.getAttribute(CMConstant.LOGINUSER);
if (obj==null||"".equals(obj.toString())) {
response.sendRedirect(request.getSession().getServletContext().getContextPath()+LOGIN_URL;
return false;
}
return true;
}
Step 5: configure the friendly tips page sessionrun.jsp
<body>
<SCRIPT language="JavaScript">
alert(" The user has logged in elsewhere, please log in again. ");
setTimeout(function () {
window.top.location.href="<%=path%>/index.jsp";
},2000);
</script>
</body>
This is the end of springMvc intercept session post-failure processing.