之前项目的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. Leetcode 671.二叉树中第二小的节点

    二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...

  2. Scala 基础(8)—— 占位符_和部分应用函数

    1. 占位符 _ 针对 Scala 基础(7)—— 函数字面量和一等函数 中最后提到的,关于 filter 函数字面量的精简写法,可以使用占位符 _ 来代替. 占位符_,用来表示一个或者多个参数.(这 ...

  3. 【bzoj3894】文理分科 网络流最小割

    原文地址:http://www.cnblogs.com/GXZlegend 题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用 ...

  4. [codeforces] 449C Jzzhu and Apples

    原题 质因数分解后贪心即可(最后贪2) #include<cstdio> #include<vector> #include<stack> #include< ...

  5. 雅礼集训 Day5 T3 题 解题报告

    题 题目背景 由于出题人赶时间所以没办法编故事来作为背景. 题目描述 一开始有\(n\)个苹果,\(m\)个人依次来吃苹果,第\(i\)个人会尝试吃\(u_i\)或\(v_i\)号苹果,具体来说分三种 ...

  6. 【HDU 1358 Period】

                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)     ...

  7. webapp的favicon应该怎样组织代码

    处理过那么多index 页面了那么关于特别针对于此页的favicon是时候详细的总结一下了 它是网站的头像,它出现在浏览器的收藏夹中(标题的旁边) ,浏览器标签页的左上角,微信公众号的logo,保存网 ...

  8. 解决PKIX path building failed

    起因 上周在生产环境部署时,把安全证书加到k8s-ingress中时发现报该错误 解决 找网上解决方案,因为这种问题相对比较少见,也没百度,直接谷歌,找到解决方案如下:https://stackove ...

  9. HDU5037 Frog

    Once upon a time, there is a little frog called Matt. One day, he came to a river. The river could b ...

  10. Hibernate中双向多对多的两种配置方式

    Hibernate中双向多对多的两种配置方式 1.建立多对多双向关联关系 package cn.happy.entitys; import java.util.HashSet; import java ...