博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot中定义拦截器
阅读量:4452 次
发布时间:2019-06-07

本文共 1540 字,大约阅读时间需要 5 分钟。

 

首先回忆一下springmvc中拦截器的使用:

1.定义一个类 implements HandlerInterceptor,实现HandlerInterceptor接口中的方法

  preHandler               1

  postHandler              2

  afterCompletion        3

2.配置拦截器 springmvc.xml

  
    
    
  

springboot 中不建议使用xml文件,在使用拦截器的时候springboot框架自动配置

springboot中定义和使用拦截器如下:

1.开发自定义拦截器类

拦截器类 implements HandlerInterceptor

public class MyInterceptor implements HandlerInterceptor {    @Override    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {        System.out.println("=======1=======");        return true;    }    @Override    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {        System.out.println("========2========");    }    @Override    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {        System.out.println("==========3========");    }}

 

2.配置拦截器,springboot自动配置(@Configuration)

@Configurationpublic class MyWebConfig extends WebMvcConfigurerAdapter {    @Override    public void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(new MyInterceptor())                .addPathPatterns("/user/test")       //拦截项目中的哪些请求                .excludePathPatterns("/user/save");  //对项目中的哪些请求不拦截    }}

 

转载于:https://www.cnblogs.com/lkldeblog/p/10636472.html

你可能感兴趣的文章
数据预处理:独热编码(One-Hot Encoding)
查看>>
python将对象名的字符串类型,转化为相应对象的操作方法
查看>>
如何删除Dead状态的container
查看>>
【NLP新闻-2013.06.03】New Book Where Humans Meet Machines
查看>>
mongodb安装4.0(rpm)
查看>>
DispatcherServlet的url mapping为“/”时,对根路径访问的处理
查看>>
备忘pwnable.kr 之passcode
查看>>
好久没敲代码了,手有点生——一个小小的时钟
查看>>
运算符 AS和IS 的区别
查看>>
(转)详解C中volatile关键字
查看>>
easyui时的时间格式yyyy-MM-dd与yyyy-MM-ddd HH:mm:ss
查看>>
专题:动态内存分配----基础概念篇
查看>>
Codeforces Round #426 (Div. 2) (A B C)
查看>>
The Most Simple Introduction to Hypothesis Testing
查看>>
UVA10791
查看>>
P2664 树上游戏
查看>>
jQuery 停止动画
查看>>
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
MyBatis Generator去掉生成的注解
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>