Spring MVC 拦截器系列-会话超时拦截器

工作已经七个年头,一直都在为别人或者为公司服务,近段时间突然想把自己这几年积累的东西写出来,这样一方面对于自己的文笔是一种锻炼,另一方面也可以加强自己的记忆,同时还能给未遇到相同的问题的朋友提供帮助。

好了,费话就不多说了,进入今天的主题——Spring MVC拦截器-会话超时拦截器

所谓拦截器,从字面意思不难理解,作用就是对于行为或者动作进行拦截。

SpringMVC 拦截器概述

    SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的。在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方式是要定义的Interceptor类要实现了Spring 的HandlerInterceptor 接口,或者是这个类继承实现了HandlerInterceptor 接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter ;第二种方式是实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。

1. 要使用拦截器,第一步就必须要引入SpringMVC相关的Jar文件,同时还需要引入aopalliance.jar文件;

2. 定义拦截器类:SessionTimeoutInterceptor

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.jack.lanqiubus.common.constant.SiteConstant;
import com.jack.lanqiubus.model.admin.SysUserModel;

/**
 * @description: 处理session超时的拦截器 会话控制拦截器
 * 
 */
public class SessionTimeoutInterceptor  implements HandlerInterceptor{
    
    public String[] allowUrls;//还没发现可以直接配置不拦截的资源,所以在代码里面来排除

    @Autowired
    private ResourceBundleMessageSource _res;
    
    public void setAllowUrls(String[] allowUrls) {
        this.allowUrls = allowUrls;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object arg2) throws Exception {
        String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");  
        if(null != allowUrls && allowUrls.length>=1)
            for(String url : allowUrls) {  
                if(requestUrl.contains(url)) {
                    return true;  
                }  
            }
        
        SysUserModel user = (SysUserModel) request.getSession().getAttribute(SiteConstant.AUTHENTICATION_KEY);
        if(user != null) {  
            return true;  //返回true,则这个方面调用后会接着调用postHandle(),  afterCompletion()
        }else{
            /**
             * 如果无法忍受每次都抛出异常时,可以使用此方法进行页面跳转
             */
//            PrintWriter out = response.getWriter();  
//            out.println("{"statusCode":"301", "message":""+this.getMessage(request,"msg.session.invalid")+""}");
//            String page = request.getContextPath() + "/views/admin/ajaxDone.jsp?statusCode=301&message="+this.getMessage(request,"msg.session.invalid");
//            response.sendRedirect(page);
//            return false;
            // 未登录  跳转到登录页面
            throw new SessionTimeoutException();//返回到配置文件中定义的路径
        }
    }
    
    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
    }
    
    protected String getMessage(HttpServletRequest request, String code, Object... args) {
        //HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        Locale locale = localeResolver.resolveLocale(request);

        return _res.getMessage(code, args, locale);
    }

}

3. 会话超时异常捕获类:SessionTimeoutException此类可以直接新建一个类并继承自Exception就OK了,不需要做其他方法的声明,如果有需要做的特殊处理可以自行添加。

public class SessionTimeoutException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

4. 上面2,3两部已经把拦截器进行了定义,如果要使用拦截器进行正常工作的话,还需要进行适合的配置,配置如下[拦截器代码需要添加在Spring的配置文件中]:

<!-- 进行登录拦截  [-->
    <!-- 拦截器配置 -->
    <mvc:interceptors>
      <!-- session超时 -->
      <mvc:interceptor>
        <mvc:mapping path="/*/*/*"/><!-- 此处将拦截所有包含三层路径的所有URL请求,可以根据个人使用的实际情况进行修改 -->
        <bean class="com.jack.lanqiubus.admin.SessionTimeoutInterceptor">
          <property name="allowUrls">
            <list>
              <!-- 如果请求中包含以下路径,则不进行拦截 -->
              <value>/login</value>
              <value>/loginDialog</value>
              <value>/logout</value>
              <value>/js</value>
              <value>/css</value>
              <value>/image</value>
              <value>/images</value>
            </list>
          </property>
        </bean>
      </mvc:interceptor>
    </mvc:interceptors>
    
    <!-- 自定义异常处理,SimpleMappingExceptionResolver这个类可以是个空类,但是要写,方便在java代码里面使用 -->
      <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      <property name="exceptionMappings">
        <props>
          <prop key="com.jack.lanqiubus.admin.SessionTimeoutException">redirect:/admin/transfer</prop>
        </props>
      </property>
    </bean>
    <!--] 进行登录拦截  -->

其中异常捕获后的 redirect:/admin/transfer  此处为Spring MVC要进行跳转登录页面地址,可自行进行配置处理。

个人网站地址:http://www.52cfml.com/post/Springmvc_SessionTimeoutInterceptor.html

后续会推出:

Spring MVC 拦截器系列-权限控制拦截器

原文地址:https://www.cnblogs.com/whyloverjack/p/Springmvc_SessionTimeoutInterceptor.html