拦截器是指通过统一拦截从浏览器发往服务器的请求来完成功能的增强
应用场景:解决一些共性问题,比如权限验证、乱码等
spring boot中使用拦截器:
1、创建一个类MyWebConfig继承WebMvcConfigurerAdapter,并重写addInterceptors方法
@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Autowired
MyiInterceptor myiInterceptor;
/**
*注册 拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//多个拦截器组成一个拦截器链
// addPathPatterns添加拦截规则
// excludePathPatterns排除拦截
//拦截器myiInterceptor只拦截'/111'的请求,不拦截'/helloWorld'
registry.addInterceptor(myiInterceptor).addPathPatterns("/111").excludePathPatterns("/helloWorld");
super.addInterceptors(registry);
}
}
2、创建一个自定义拦截器MyiInterceptor实现HandlerInterceptor接口,重写所有的方法实现自己的业务
@Component
public classMyiInterceptorimplementsHandlerInterceptor {
/**
*返回值为true请求会继续执行,false请求终止
*@paramhttpServletRequest请求请求
*@paramhttpServletResponse响应对象
*@paramo被拦截的对象
*@return
*@throwsException
*/
@Override
public booleanpreHandle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Object o)throwsException {
System.out.println("preHandle方法执行了。。。");
httpServletRequest.setCharacterEncoding("utf-8");//设置请求编码
httpServletResponse.setCharacterEncoding("utf-8");//设置响应编码
//示例 没有登录时, 转发到登录页 返回false中断请求
//httpServletRequest.getRequestDispatcher("/login.html").forward(httpServletRequest,httpServletResponse);
//return false;
return true;
}
/**
*
*@paramhttpServletRequest请求请求
*@paramhttpServletResponse响应对象
*@paramo被拦截的对象
*@parammodelAndView可以在这个对象中设置返回的视图和试图内容
*@throwsException
*/
@Override
public voidpostHandle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Object o,ModelAndView modelAndView)throwsException {
System.out.println("postHandle方法执行了。。。。");
}
/**
*请求执行完销毁数据
*@paramhttpServletRequest请求请求
*@paramhttpServletResponse响应对象
*@paramo被拦截的对象
*@parame
*@throwsException
*/
@Override
public voidafterCompletion(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Object o,Exception e)throwsException {
System.out.println("afterCompletion方法执行了。。。");
}
}
多拦截器工作流程:
拦截器和过滤器的区别:
过滤器Filter依赖servlet容器,基于回调函数,作用范围大
拦截器Interceptor依赖框架容器,基于反射机制,只过滤请求