springboot 配置使用拦截器 interceptor
来源:互联网 发布:华为软件测试招聘 编辑:程序博客网 时间:2024/05/21 13:14
springboot 配置使用拦截器 interceptor
1 定义拦截器类实现 HandlerInterceptor
package com.kerry.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class MyInterceptor implements HandlerInterceptor {@Overridepublic void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2, ModelAndView arg3) throws Exception {// TODO Auto-generated method stub}@Overridepublic boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2) throws Exception {return false;}}
2 编写配置类 InterceptorConfigure
package com.kerry.configure;import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.kerry.interceptor.MyInterceptor;import com.kerry.interceptor.MyInterceptor1;@Configurationpublic class InterceptorConfigure extends WebMvcConfigurerAdapter {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new MyInterceptor()).addPathPatterns("/interceptor/*").excludePathPatterns("/interceptor/exclude/*");//registry.addInterceptor(new MyInterceptor1())//.addPathPatterns("/interceptor1/*")//.excludePathPatterns("/interceptor1/exculde/*");}}
多个拦截器可以在里面添加多个
上面继承的 WebMvcConfigurerAdapter 里面有很多其他的使用配置
只有经过DispatcherServlet 的请求,才会走拦截器链,我们自定义的Servlet 请求是不会被拦截的,比如我们自定义的Servlet地址 http://localhost:8080/servlet/myServlet 是不会被拦截器拦截的。并且不管是属于哪个Servlet 只要复合过滤器的过滤规则,过滤器都会拦截。
上面用到的 WebMvcConfigurerAdapter 并非只是注册添加拦截器使用,其顾名思义是做Web配置用的,它还可以有很多其他作用,通过下面截图便可以大概了解,具体每个方法都是干什么用的,留给大家自己研究(其实都大同小异也很简单)
附上项目结构
阅读全文
0 0
- springboot 配置使用拦截器 interceptor
- SpringBoot之Interceptor拦截器
- springboot自定义拦截器Interceptor
- SpringMVC interceptor拦截器配置使用(源码)
- SpringMvc Interceptor拦截器的配置与使用
- 拦截器Interceptor的使用
- Hibernate 拦截器Interceptor使用
- springMVC使用interceptor拦截器
- springboot 拦截器配置
- springboot使用拦截器
- 解决Springboot中Interceptor拦截器中依赖注入失败
- Spring mvc interceptor配置拦截器
- Spring mvc interceptor配置拦截器
- Spring mvc interceptor配置拦截器
- SPRING MVC interceptor配置拦截器
- Spring mvc 拦截器 interceptor 配置
- SpringMVC mvc:interceptor拦截器配置
- Spring mvc interceptor配置拦截器
- hpuoj 【1067】拍七游戏【水题】&&【思维】
- python中的字符串操作
- Java文件操作大全
- 指针常量?常量指针?
- Qt 布局管理器
- springboot 配置使用拦截器 interceptor
- APK Signature Scheme v2
- JS改变input的value值不触发onchange事件解决方案
- 玩转树莓派之在树莓派Jessie系统中安装ROS
- Android数据存储-DB(SQLite)
- 欢迎使用CSDN-markdown编辑器
- Codeforces Round #426 (Div. 2) A B C The Meaningless Game
- js高级教程阅读笔记 第二章-在HTML中使用JavaScript
- android子线程怎样向主线程传递集合