Spring拦截器

谁说我不能喝 提交于 2019-12-23 20:50:23

一、拦截器(1)

  一)、HandlerInterceptor(拦截器)

   ☆☆☆定义实现类

    方式一:类实现Spring的HandlerInterceptor接口

    方式二:类继承实现了HandlerInterceptor接口的类,如实现了HandlerInterceptor接口的抽象类 →    HandlerInterceptorAdapter

   ☆☆☆方式介绍

    ①、boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws  Exception; //在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等;

    ②、void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception; //在业务处理器处理请求完成之后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但没有渲染),可以对ModelAndView做更改

    ③、void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception; //在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面)

   ☆☆☆进阶使用(这里引用别的博主的内容)

    //https://blog.csdn.net/zhibo_lv/article/details/81738940

    //https://blog.csdn.net/zhibo_lv/article/details/81875705 //解决在拦截器中通过request.getInputStream();获取到body中的信息后,在controller中使用了@RequestBody注解报错 I/O error while reading input message; nested exception is java.io.IOException: Stream closed

    //https://blog.csdn.net/zhibo_lv/article/details/81905300 //防止重复提交

  

  ★★★★★子类(非完全相同与Java中的子类的定义,请注意区分)

   1、接口 AsyncHandlerInterceptor 直接继承HandlerInterceptor

    /*

    * 相对于HandlerInterceptor,新增方法

    * 该方法会在Controller方法异步执行时先支持preHandle(),然后此方法开始执行,而HandlerInterceptor的postHandle()需要等Controller异步执行完才执行

    */

    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object   handler) throws Exception { }

 

    2、抽象类HandlerInterceptorAdapter 拦截器适配器,需要继承HandlerInterceptor,由于AsyncHandlerInterceptor 直接继承HandlerInterceptor,所以HandlerInterceptorAdapter 可以直接实现AsyncHandlerInterceptor

    public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor {

     /**

     * 默认是true

     */

     @Override

     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; }

     /**

     * This implementation is empty.

     */

     @Override

     public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { }

     /**

     * This implementation is empty.

     */

     @Override

     public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }

     /**

     * 不是HandlerInterceptor的接口实现,是AsyncHandlerInterceptor的,AsyncHandlerInterceptor实现了HandlerInterceptor

     */

     @Override

     public void afterConcurrentHandlingStarted( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { }

     }

 

   二)、InterceptorRegistry (拦截器注册类登记类)

    //org.springframework.web.servlet.config.annotation.InterceptorRegistry

    方法

     1、addInterceptor方法

      /**

      * 添加提供的{@link HandlerInterceptor}。

      * 拦截器用于添加@return {@link InterceptorRegistration},允许您进一步配置已注册的拦截器,

      * 例如添加它应该应用到的URL模式。

      */

      public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {

        InterceptorRegistration registration = new InterceptorRegistration(interceptor);

        this.registrations.add(registration);

        return registration;

      }

 

     2、addWebRequestInterceptor方法

      /**

      * 添加提供的{@link WebRequestInterceptor}。

      * 拦截器用于添加@return {@link InterceptorRegistration},允许您进一步配置已注册的拦截器,

      * 例如添加它应该应用到的URL模式。

      */

      public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor) {

        WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor);

        InterceptorRegistration registration = new InterceptorRegistration(adapted);

        this.registrations.add(registration);

        return registration;

       }

 

     3、getInterceptors方法

       /**

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

       */

       protected List<Object> getInterceptors() {

         return this.registrations.stream()

.         sorted(INTERCEPTOR_ORDER_COMPARATOR)

.         map(InterceptorRegistration::getInterceptor)

         .collect(Collectors.toList());

       }

 

     4、

       /*

       * 拦截器注册类集合

       */

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

 

     5、

       /*

       * 用于拦截器顺序排序规则

       * 自定义规则

       */

       private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR =

         OrderComparator.INSTANCE.withSourceProvider(object -> {

           if (object instanceof InterceptorRegistration) {

           return (Ordered) ((InterceptorRegistration) object)::getOrder;

         }

        return null;

       });

//以上为根据公司项目加上各个博主博客内容的综合。暂时先给到个开头,还有续集。如有相同、有误,还请指出。

//请勿喷,谢谢。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!