之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的。

以下WebMvcConfigurerAdapter 比较常用的重写接口
    /** 解决跨域问题 **/
    public void addCorsMappings(CorsRegistry registry) ;
    /** 添加拦截器 **/
    void addInterceptors(InterceptorRegistry registry);
    /** 这里配置视图解析器 **/
    void configureViewResolvers(ViewResolverRegistry registry);
    /** 配置内容裁决的一些选项 **/
    void configureContentNegotiation(ContentNegotiationConfigurer configurer);
    /** 视图跳转控制器 **/
    void addViewControllers(ViewControllerRegistry registry);
    /** 静态资源处理 **/
    void addResourceHandlers(ResourceHandlerRegistry registry);
    /** 默认静态资源处理器 **/

void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

但是在新项目中,使用的是springboot 2.0.3,在使用时提示该类已经过时(在类上加这个注解@Deprecated,就会提示这个类已经过时),这个果断不能忍啊,去网上百度了一下,说是springboot 2.0.0以上和spring 5.0以上的版本这个类会被废弃,将使用新的类(WebMvcConfigurationSupport)来代替,这个类是WebMvcConfigurerAdapter的扩展和替代。

这是我代码中的使用案例:

package com.*.ding.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 自定义拦截器
* @Auther: Tt(yehuawei)
* @Date: 2018/7/11 14:43
*/
@Component
public class MyInterceptor implements HandlerInterceptor { //实现原生拦截器的接口 @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//进行逻辑判断,如果ok就返回true,不行就返回false,返回false就不会处理改请求
return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception { }
}
package com.*.ding.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /**
* 拦截器配置
*
*
* @Auther: Tt(yehuawei)
* @Date: 2018/7/11 15:05
*/
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
// 以下WebMvcConfigurerAdapter 比较常用的重写接口
// /** 解决跨域问题 **/
// public void addCorsMappings(CorsRegistry registry) ;
// /** 添加拦截器 **/
// void addInterceptors(InterceptorRegistry registry);
// /** 这里配置视图解析器 **/
// void configureViewResolvers(ViewResolverRegistry registry);
// /** 配置内容裁决的一些选项 **/
// void configureContentNegotiation(ContentNegotiationConfigurer configurer);
// /** 视图跳转控制器 **/
// void addViewControllers(ViewControllerRegistry registry);
// /** 静态资源处理 **/
// void addResourceHandlers(ResourceHandlerRegistry registry);
// /** 默认静态资源处理器 **/
// void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); @Autowired
private MyInterceptor myInterceptor; /**
*
* 功能描述:
* 配置静态资源,避免静态资源请求被拦截
* @auther: Tt(yehuawei)
* @date:
* @param:
* @return:
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**")
.addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
} public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor)
//addPathPatterns 用于添加拦截规则
.addPathPatterns("/**")
//excludePathPatterns 用于排除拦截
//注意content-path:ding是不用填写的
//项目启动测试接口
.excludePathPatterns("/")
//钉钉回调事件
.excludePathPatterns("/callback/**")
//检查验证码
.excludePathPatterns("/check_auth_code")
//发送验证码
.excludePathPatterns("/send_auth_code")
//获取用户基本信息
.excludePathPatterns("/get_user_base_info")
//获取用户详情
.excludePathPatterns("/get_user_detail_info");
super.addInterceptors(registry);
}
}

springboot 2.0+ 自定义拦截器的更多相关文章

  1. SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦截问题

      自定义拦截器 /** * UserSecurityInterceptor * Created with IntelliJ IDEA. * Author: yangyongkang * Date: ...

  2. SpringBoot中设置自定义拦截器

    SpringBoot中设置自动以拦截器需要写一个类继承HandlerInterceptorAdapter并重写preHandle方法 例子 public class AuthorityIntercep ...

  3. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  4. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  5. SpringBoot学习笔记:自定义拦截器

    SpringBoot学习笔记:自定义拦截器 快速开始 拦截器类似于过滤器,但是拦截器提供更精细的的控制能力,它可以在一个请求过程中的两个节点进行拦截: 在请求发送到Controller之前 在响应发送 ...

  6. SpringBoot(11) SpringBoot自定义拦截器

    自定义拦截器共两步:第一:注册.第二:定义拦截器. 一.注册 @Configuration 继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本) 旧版本代码 ...

  7. SpringBoot自定义拦截器实现

    1.编写拦截器实现类,此类必须实现接口   HandlerInterceptor,然后重写里面需要的三个比较常用的方法,实现自己的业务逻辑代码 如:OneInterceptor package com ...

  8. 【学习】SpringBoot之自定义拦截器

    /** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...

  9. SpringBoot之自定义拦截器

    一.自定义拦截器实现步骤 1.创建拦截器类并实现HandlerInterceptor接口 2.创建SpringMVC自定义配置类,实现WebMvcConfigurer接口中addInterceptor ...

随机推荐

  1. json序列化datetime类型数据

    错误描述: import jsonimport datetime a = datetime.datetime.now()print(a) b = json.dumps(a)print(b) 如上代码, ...

  2. Qt Creator : Read an image from resources

    最近两周碰到的一个问题是: opencv无法读取qt中的资源文件. 参考网址:https://stackoverflow.com/questions/45233559/qt-creator-read- ...

  3. js万年历

    首先,注意: 1.延迟执行     window.setTimeout(    ,     )     里面的时间是以毫秒计算的 2.间隔执行    window.setInterval(     , ...

  4. shell中的>&1和 >&2是什么意思?

    当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...

  5. 将MSHFlexGrid1中记录导出为Excel

    1.添加引用Microsoft Excel 14.0 Object Library 2.编写代码部分 Private Sub Output_Click() Dim i As Integer '定义变量 ...

  6. java GUI Graphics2D 绘图

    Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...

  7. [SDOI2017][bzoj4817] 树点涂色 [LCT+线段树]

    题面 传送门 思路 $LCT$ 我们发现,这个1操作,好像非常像$LCT$里面的$Access$啊~ 那么我们尝试把$Access$操作魔改成本题中的涂色 我们令$LCT$中的每一个$splay$链代 ...

  8. 深入解析VueJs中的V-bind指令

    v-bind 主要用于属性绑定,比方你的class属性,style属性,value属性,href属性等等,只要是属性,就可以用v-bind指令进行绑定.这次主要介绍了VueJs中的V-bind指令,需 ...

  9. Linux系统——提高编译速度的方法

    编译优化: 基本原则就是“以空间换时间” tmpfs: 解决IO瓶颈,充分利用本机内存资源 make -j: 充分利用本机计算资源 distcc: 利用多台计算机资源 ccache: 减少重复编译相同 ...

  10. Tomcat的context.xml说明、Context标签讲解

    Tomcat的context.xml说明.Context标签讲解 1. 在tomcat 5.5之前 --------------------------- Context体现在/conf/server ...