zoukankan      html  css  js  c++  java
  • 在springboot中使用拦截器

    在springMVC中可以实现拦截器,是通过实现HandlerInterceptor接口,然后在springmvc-web.xml中配置就可以使用拦截器了。在springboot中拦截器也是一样的思想,使用方法还是没有变,只不过是配置稍微变了一下。

    在springboot中使用拦截器步骤如下:

    1.按照springmvc模式写一个拦截器类

    和springmvc一样,也要写一个类实现HandlerInterceptor接口,然后重新其中的prehandle方法。

    2.然后写一个配置类,继承WebMvcConfigureAdapter(这个方法已经过时了)或者实现WebMvcConfigurer接口,覆盖里面的方法 并且在类上添加注解@Configuration

    如下所示:

    @Configuration
    public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    //需要拦截的路径,通常都是多个,所以使用数组形式
    String[] addPathPatterns = {
    "/hellojsp"
    };
    //不需要的拦截路径,同上
    String[] excludePathPatterns = {
    "/hello/boot"
    };
    //可以将添加拦截的路径和不需要拦截的路径都写在一行上。如果有多个,就写多行
    registry.addInterceptor(new MyInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
    }
    }

    如上,就可以在springboot中使用拦截器了。

  • 相关阅读:
    Element+Vue.js 选择器常用属性
    常用xml头文件
    【链接】调查显示:超20%美国大学生曾花学生贷款投
    Quartz遇到的问题
    List去重
    SpringDataJPA
    IDEA:Error during artifact deployment. See server log for details.详解
    Quartz定时任务
    多线程条件通行工具——CountDownLatch
    多线程同步工具——Lock
  • 原文地址:https://www.cnblogs.com/jasonboren/p/11140929.html
Copyright © 2011-2022 走看看