首先在web.xml中配置spring mvc入口:DispatcherServlet

SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet。
    DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller来处理。  所以我们现在web.xml中加入以下配置:

<servlet><!--springMVC Config-->
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!--这里是新建的spring mvc配置文件-->
        <param-value>classpath*:/spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <!-- 所有的的请求,都会被DispatcherServlet处理 -->
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!--End springMVC Config-->
<servlet><!--springMVC Config-->
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!--这里是新建的spring mvc配置文件-->
        <param-value>classpath*:/spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <!-- 所有的的请求,都会被DispatcherServlet处理 -->
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!--End springMVC Config-->

2、对静态资源不拦截

  如果只配置拦截类似于*.do格式的url,则对静态资源的访问是没有问题的,但是如果配置拦截了所有的请求(如我们上面配置的“/”),就会造成js文件、css文件、图片文件等静态资源无法访问。
      一般实现拦截器主要是为了权限管理,主要是拦截一些url请求,所以不对静态资源进行拦截。要过滤掉静态资源一般有两种方式,
      第一种是采用<mvc:default-servlet-handler />,(一般Web应用服务器默认的Servlet名称是"default",所以这里我们激活Tomcat的defaultServlet来处理 静态文件,在web.xml里配置如下代码即可:)

<!-- 不拦截静态文件 -->  
<servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>/js/*</url-pattern>  
    <url-pattern>/css/*</url-pattern>  
    <url-pattern>/images/*</url-pattern>  
    <url-pattern>/fonts/*</url-pattern>  
</servlet-mapping>
<!-- 不拦截静态文件 -->  
<servlet-mapping>  
    <servlet-name>default</servlet-name>  
    <url-pattern>/js/*</url-pattern>  
    <url-pattern>/css/*</url-pattern>  
    <url-pattern>/images/*</url-pattern>  
    <url-pattern>/fonts/*</url-pattern>  
</servlet-mapping>

第二种方法:

采用<mvc:resources />,在springmvc的配置文件中加入以下代码:

<mvc:resources mapping="/js/**" location="/global/js/"/>
<mvc:resources mapping="/styles/**" location="/global/css/"/>
<mvc:resources mapping="/images/**" location="/global/images/"/>
<mvc:resources mapping="/js/**" location="/global/js/"/>
<mvc:resources mapping="/styles/**" location="/global/css/"/>
<mvc:resources mapping="/images/**" location="/global/images/"/>

3、自定义拦截器    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法。preHandle在业务处理器处理请求之前被调用,
    postHandle在业务处理器处理请求执行完成后,生成视图之前执行,afterCompletion在DispatcherServlet完全处 理完请求后被调用,可用于清理资源等 。所以要想实现自己的权限管理逻辑,需要继承HandlerInterceptorAdapter并重写其三个方法。
    首先在springmvc.xml中加入自己定义的拦截器我的实现逻辑CommonInterceptor,

<!--配置springMVC拦截器, 多个拦截器,顺序执行 -->
<mvc:interceptors>
    <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
    <!--<bean class=""></bean>-->
    <mvc:interceptor>
        <!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
        <mvc:mapping path="/api/**" />
        <bean class="com.jason.system.CommonInterceptor"/>
    </mvc:interceptor>
    <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>
<!--配置springMVC拦截器, 多个拦截器,顺序执行 -->
<mvc:interceptors>
    <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
    <!--<bean class=""></bean>-->
    <mvc:interceptor>
        <!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
        <mvc:mapping path="/api/**" />
        <bean class="com.jason.system.CommonInterceptor"/>
    </mvc:interceptor>
    <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>

4、最后实现类:CommonInterceptor

package com.jason.system;

import com.jason.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**公共拦截器
 * Created by liupu on 2015/10/26.
 */
public class CommonInterceptor implements HandlerInterceptor {
    private  static final Logger logger= LoggerFactory.getLogger(CommonInterceptor.class);

    /**
     * 在请求之前拦截,返回true则进入请求Controller
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

        logger.info("==============执行顺序: 1、preHandle================");
        String requestUri = request.getRequestURI();
        String method = request.getMethod();
        String url = request.getContextPath();
        logger.info("RequestUri:"+requestUri);
        logger.info("method:"+method);
        logger.info("url:"+url);


//       String username =  (String)request.getSession().getAttribute("user");
//        if(username == null){
//            request.getRequestDispatcher("/tool/login/login.jsp").forward(request, response);
//            return false;
//        }else{return true;}

        return true;
    }


    /**
     * 在业务处理器处理请求执行完成后,在返回试图前拦截,
     * 可在modelAndView中加入数据,比如当前时间
     * @param response
     * @param request
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

        logger.info("==============执行顺序: 2、postHandle================");
        if (null!=modelAndView){
            modelAndView.addObject("serverTime", DateUtils.getDate(DateUtils.YYYY_MM_DD_HH_mm_ss));
        }
    }


    /**
     * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     * @param request
     * @param response
     * @param e
     * @throws Exception
     */



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
        logger.info("==============执行顺序: 3、afterCompletion================");
    }
}
package com.jason.system;

import com.jason.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**公共拦截器
 * Created by liupu on 2015/10/26.
 */
public class CommonInterceptor implements HandlerInterceptor {
    private  static final Logger logger= LoggerFactory.getLogger(CommonInterceptor.class);

    /**
     * 在请求之前拦截,返回true则进入请求Controller
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

        logger.info("==============执行顺序: 1、preHandle================");
        String requestUri = request.getRequestURI();
        String method = request.getMethod();
        String url = request.getContextPath();
        logger.info("RequestUri:"+requestUri);
        logger.info("method:"+method);
        logger.info("url:"+url);


//       String username =  (String)request.getSession().getAttribute("user");
//        if(username == null){
//            request.getRequestDispatcher("/tool/login/login.jsp").forward(request, response);
//            return false;
//        }else{return true;}

        return true;
    }


    /**
     * 在业务处理器处理请求执行完成后,在返回试图前拦截,
     * 可在modelAndView中加入数据,比如当前时间
     * @param response
     * @param request
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

        logger.info("==============执行顺序: 2、postHandle================");
        if (null!=modelAndView){
            modelAndView.addObject("serverTime", DateUtils.getDate(DateUtils.YYYY_MM_DD_HH_mm_ss));
        }
    }


    /**
     * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     * @param request
     * @param response
     * @param e
     * @throws Exception
     */



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
        logger.info("==============执行顺序: 3、afterCompletion================");
    }
}