spring 拦截器

1,什么是拦截器

       处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理。例如登陆,判断是否登陆,记录日志等

2,spring boot 配置拦截器

 @Configuration
 public class MyWebAppConfigurer  extends WebMvcConfigurerAdapter{
      public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(new MyInterceptor()); // 多个拦截器组成一个拦截器链
          super.addInterceptors(registry);
    }
}

拦截器登记

它允许您随意配置 进一步注册拦截器,例如添加它应该应用于的URL模式

 private final List<InterceptorRegistration> registrations = new ArrayList<InterceptorRegistration>();

public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
        InterceptorRegistration registration = new InterceptorRegistration(interceptor);
        this.registrations.add(registration);
        return registration;
    }

(1) @Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方        法  将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器
(2)注册拦截器类

       实现 HandlerInterceptor (处理拦截器链)

public class MyInterceptor   implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("开始1");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("开始2");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("开始3");
        
    }

}

接下讲下这 (1) preHandle (2) postHandle  (3) afterCompletion 三个方法,在spring mvc 的的调用法

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpServletRequest processedRequest = request;
        HandlerExecutionChain mappedHandler = null;
        boolean multipartRequestParsed = false;

        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

        try {
            ModelAndView mv = null;
            Exception dispatchException = null;

            try {
                processedRequest = checkMultipart(request);
                multipartRequestParsed = (processedRequest != request);

              
              //确定当前请求的处理程序。
                mappedHandler = getHandler(processedRequest);
                if (mappedHandler == null || mappedHandler.getHandler() == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }

                //确定当前请求的处理程序适配器。
                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

               如果处理程序支持,则处理最后修改的标头。
                String method = request.getMethod();
                boolean isGet = "GET".equals(method);
                if (isGet || "HEAD".equals(method)) {
                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                    if (logger.isDebugEnabled()) {
                        logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
                    }
                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }

              //这里开始调用拦截器

                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }

                 //实际调用处理程序。
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }

                applyDefaultViewName(processedRequest, mv);
                mappedHandler.applyPostHandle(processedRequest, response, mv);
            }
            catch (Exception ex) {
                dispatchException = ex;
            }
            catch (Throwable err) {
                 处理处理程序方法抛出的错误,
                // 使它们可用于@ExceptionHandler方法和其他场景。
                dispatchException = new NestedServletException("Handler dispatch failed", err);
            }
            processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
        }
        catch (Exception ex) {
            triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
        }
        catch (Throwable err) {
            triggerAfterCompletion(processedRequest, response, mappedHandler,
                    new NestedServletException("Handler processing failed", err));
        }
        finally {
            if (asyncManager.isConcurrentHandlingStarted()) {
                // Instead of postHandle and afterCompletion
                if (mappedHandler != null) {
                    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
                }
            }
            else {
                // Clean up any resources used by a multipart request.
                if (multipartRequestParsed) {
                    cleanupMultipart(processedRequest);
                }
            }
        }
    }

* @return {@code true}如果执行链继续执行 *下一个拦截器或处理程序本身。别的,DispatcherServlet假设 *这个拦截器已经处理了响应本身。

boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
     HandlerInterceptor[] interceptors = getInterceptors();//获得所有的拦截器
        if (!ObjectUtils.isEmpty(interceptors)) {
            for (int i = 0; i < interceptors.length; i++) {
                HandlerInterceptor interceptor = interceptors[i];
                if (!interceptor.preHandle(request, response, this.handler)) {//执行拦截器的每个方法
                    triggerAfterCompletion(request, response, null);
                    return false;
                }
                this.interceptorIndex = i;
            }
        }
        return true;
    }

 返回所有已注册的拦截器。

 protected List<Object> getInterceptors() {
        List<Object> interceptors = new ArrayList<Object>(this.registrations.size());
        for (InterceptorRegistration registration : this.registrations) {
            interceptors.add(registration.getInterceptor());
        }
        return interceptors ;
    }

    InterceptorRegistration类

    private final HandlerInterceptor interceptor;//拦截器

    private final List<String> includePatterns = new ArrayList<String>();//拦截的请求URL

    private final List<String> excludePatterns = new ArrayList<String>();//不拦截的URL

    private PathMatcher pathMatcher;

(1)拦截器执行的顺序

        preHandle 处理请求之前

        postHandle 渲染页面之前

        afterCompletion 处理请求之后

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值