Spring mvc Interceptor 解决Session超时跳转
来源:互联网 发布:北京java认证资格培训 编辑:程序博客网 时间:2024/06/06 19:02
Spring mvc Interceptor 解决Session超时
1:在spring_xxx.xml里加入相关配置
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/*/*" /><bean class="com.teachmanage.mvc.interceptor.SessionTimeoutInterceptor" ><property name="allowUrls"> <list> <value>/login</value> <value>/login/logout</value> </list> </property> </bean></mvc:interceptor></mvc:interceptors><!-- exception handler --><bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" ><property name="exceptionMappings"><props><prop key="com.teachmanage.mvc.exception.SessionTimeoutException">redirect:/login</prop></props></property></bean>
上述配置里,list中的url为允许访问的url,即从这些url访问的话,不需要判断session是否超时。(为什么这些url能够不判断,是在下述拦截器代码里实现的。)
拦截生效后,会抛出SessionTimeoutException,跳转到/login。这里的redirect:/login是指controller里对应的url,如果想跳转到login.jsp,则直接/login。
2:创建SessionTimeoutInterceptor
public class SessionTimeoutInterceptor implements HandlerInterceptor {private List<String> allowUrls = new ArrayList<String>();public List<String> getAllowUrls() {return allowUrls;}public void setAllowUrls(List<String> allowUrls) {this.allowUrls = allowUrls;}public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String requestUrl = request.getRequestURI();for(String url : allowUrls) {if(requestUrl.endsWith(url)) {return true;}}String session = TypeConvertCommon.toString(WebUtils.getSessionAttribute(request, "User_id"));if("".equals(session)) {throw new SessionTimeoutException();} else {return true;}}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}
该拦截器需要实现HandlerInterceptor接口,否则会报错No matching editors or conversion strategy found。不过postHandle和afterComletion暂时无事可做,空着就可以了。
还需要创建一个SessionTimeoutException类,不过不需要写入代码,空着就行。
3:如果项目使用了iframe,则超时跳转时会造成登陆页面内嵌到当前页面,可以在登陆画面上加上如下js代码:
if (window != top) {top.location.href = location.href;}
- Spring mvc Interceptor 解决Session超时跳转
- Spring mvc Interceptor 解决Session超时配置流程
- Spring mvc Interceptor 解决Session超时配置流程
- Spring mvc Interceptor 解决Session超时配置流程
- Spring mvc Interceptor 解决Session超时配置流程
- 解决session超时跳转首页,跳出frameset
- 解决session超时跳转主页,跳出frameset
- session 超时 页面跳转
- session 超时 页面跳转
- session超时,跳转页面
- spring mvc interceptor 示例
- spring mvc Interceptor
- Spring MVC interceptor 使用
- spring mvc interceptor
- Spring MVC Handler Interceptor
- Spring MVC Interceptor
- spring mvc Interceptor
- spring-mvc Interceptor
- poj 2288 Islands and Bridges 状态压缩dp(附详细注释)
- LeetCode Roman to Integer 罗马数字转阿拉伯数字
- java数组整理
- iOS 触摸事件之双指先后触摸问题的解决
- Java_io体系之PrintStream简介、走进源码及示例——09
- Spring mvc Interceptor 解决Session超时跳转
- 开发代码版本管理工具
- java中URL和File的相互转化 及两者的使用场景
- UVA 10986 - Sending email(最短路 优先队列的优化+Bellman-Ford)
- linux下的sh和bash区别
- com.microsoft.sqlserver.SQLServerException:索引1超出范围
- Objective-C知识点总结(二)
- LM2575电源电路设计测试总结
- MinGW编译C++11备忘