详解springmvc控制登录用户session失效后跳转登录页面
作者:GreenRookie 时间:2021-12-08 19:36:40
springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下:
第一步,配置 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失效后处理方式结束。
来源:http://blog.csdn.net/fiangasdre/article/details/51802625
标签:spring,session,失效
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过自定制LogManager实现程序完全自定义的logger
2022-05-11 06:33:15
详解Spring Boot集成MyBatis(注解方式)
2023-10-03 17:45:47
![](https://img.aspxhome.com/file/2023/1/64481_0s.jpg)
Java基础学习之ArrayList类概述与常用方法
2023-03-13 02:15:24
Java异常 Factory method'sqlSessionFactory'rew exception;ested exception is java.lang.NoSuchMethodError:
2022-03-25 15:06:42
![](https://img.aspxhome.com/file/2023/8/86818_0s.jpg)
解决SpringBoot webSocket 资源无法加载、tomcat启动报错的问题
2021-07-28 05:06:42
java序列化对象根据不同配置动态改变属性名的方法
2022-10-06 11:31:09
C# 创建高精度定时器的示例
2023-03-09 12:35:58
![](https://img.aspxhome.com/file/2023/4/76434_0s.png)
聊聊Redis二进制数组Bitmap
2021-07-26 11:51:57
string boot 与 自定义interceptor的实例讲解
2023-10-27 17:03:20
![](https://img.aspxhome.com/file/2023/1/77171_0s.png)
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
浅谈Spring事务传播行为实战
2022-07-04 01:54:44
![](https://img.aspxhome.com/file/2023/0/86860_0s.jpg)
使用Spring Expression Language (SpEL)全面解析表达式
2021-11-19 08:07:10
![](https://img.aspxhome.com/file/2023/8/101998_0s.png)
javaweb实现app扫码登录功能
2022-03-25 03:48:19
Android设计模式系列之工厂方法模式
2023-08-16 19:10:17
![](https://img.aspxhome.com/file/2023/8/94978_0s.jpg)
C的|、||、&、&&、异或、~、!运算符
2022-05-30 13:10:30
SpringBoot实战之高效使用枚举参数(原理篇)案例详解
2022-02-10 23:54:23
![](https://img.aspxhome.com/file/2023/5/77055_0s.jpg)
Spring Boot整合Web项目常用功能详解
2023-06-04 17:14:21
C#实现观察者模式(Observer Pattern)的两种方式
2023-06-20 21:05:18
![](https://img.aspxhome.com/file/2023/0/66130_0s.png)
Android实现本地图片选择及预览缩放效果
2023-09-27 14:07:28
![](https://img.aspxhome.com/file/2023/3/93313_0s.gif)
JavaMailSender实现邮箱验证功能
2022-05-11 12:32:59
![](https://img.aspxhome.com/file/2023/8/71818_0s.png)