拦截器使用:
使用注解 @Configuration 配置拦截器
继承 WebMvcConfigurerAdapter
重写 addInterceptors 添加需要的拦截器地址
拦截器方法
拦截器按顺序拦截
Source中有自动生成重写方法
SpringBoot拦截器的使用,先使用注解@Configuration配置
继承WebMvcConfigurerAdapter
重写addInterceptors添加需要拦截其的地址
传入的Intercepter需要 implements HandlerIntercepter
@Configuration
extends WebMvcConfigurerAdapter
override addInterceptors
使用注解@Configuration表明这个类是一个配置类
实现WebMvcConfigurerAdapter表明这个类是一个拦截适配器
负责配置拦截方法
重写该接口
addPathPatterns表示这个创建类中的方法被拦截的路径
registry.addInterceptor() //将拦截器注册在拦截配置类上
大概格式:拦截器实例化对象+具体拦截哪个类
这里的拦截顺序与重写方法中的注册顺序是一样的,如果第一个注册没有放行,那么第二个注册的拦截器则不会再进行请求拦截
@Configration配置拦截器
继承WebMvcConfigurerAdapter
重写addInterceptors
@Configuration public class WebMvcConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**"); super.addInterceptors(registry); } }
public class OneInterceptor implements HandlerInterceptor { @Override//请求处理之前,Controller方法调用之前 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //System.out.println("被one拦截,放行。。。"); //return true; System.out.println("不放行!!!"); returnError(response,"不放行"); return false; }
@Override//请求处理之后,视图渲染之前。controller调用之后 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
@Override//请求结束之后,视图渲染之后,用于清理资源 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
public void returnError(HttpServletResponse response,String result) throws Exception { OutputStream out=null; try { response.setCharacterEncoding("utf-8"); response.setContentType("text/json"); out=response.getOutputStream(); out.write(result.getBytes("utf-8")); out.flush(); }finally { if(out!=null){ out.close(); } } }
60%
springboot 拦截器的使用
1. 使用 @Configuration 配置拦截器
2. 继承 WebMvcConfigurerAdapter
3.重写 addInterceptors 添加需要的拦截器地址
在class上使用注解@Configuration配置拦截器
继承WebMvcConfigurerAdapter
重写addInterceptors添加需要的拦截器地址
拦截器应用
被两个拦截器同时拦截使用:
执行顺序 按照注册顺序执行
Spring boot 拦截器的使用
使用@Configuration 认为这是一个拦截器,表明是一个适配器
继承 WebMvcConfigurerAdapter 实现适配器,并重写 addInterceptors
添加需要的拦截器地址
WebMvcConfigurerAdapter 类 有很多的方法 可以重写 以实现不同的逻辑
Interceptor 处理拦截逻辑
拦截器按照顺序执行
springboot拦截器配置
定义拦截器需要的操作:
具体实现:
需先定义一个拦截器配置类,然后重写方法,将拦截依次进行注册。
具体需要注册的拦截器内容:
springboot 拦截器的使用
SpringBoot中使用拦截器
1、SpringBoot拦截器的使用
① 使用注解@Configuration配置拦截器
② 继承WebMvcConfigurerAdapter
③ 重写addInterceptors添加需要的拦截器地址
2、实现HandlerInterceptor的拦截器
SpringBoot中使用拦截器
1、SpringBoot拦截器的使用
① 使用注解@Configuration配置拦截器
② 继承WebMvcConfigurerAdapter
③ 重写addInterceptors添加需要的拦截器地址
2、实现HandlerInterceptor的拦截器
springboot 拦截器的使用:
1、使用注解@Configuration配置拦截器
2、继承WebMvcConfigurerAdapter
3、重写addInterceptors添加需要的拦截器地址
SpringBoot拦截器的使用
SpringBoot 拦截器的使用
Spring-Boot 拦截器的使用
拦截器使用
注解、继承、重写
拦截器的使用
拦截器实现 HandlerInterceptor 接口
配置拦截器 springMvc 4.+ 继承webMvcConfigureAdapter 5.+ 直接实现 WebMvcConfiguration