目录一、拦截器执行时机二、拦截器实现方式三、指定拦截路径一、拦截器执行时机过滤器是在DispatcherServlet执行前后执行而拦截器是在RequstMapping()方法执行前后执行。所以过滤器可以处理所有请求但是拦截器只能处理DispatcherServlet的URL下的请求。二、拦截器实现方式继承HandlerInterceptorAdapter类。实现HandlerInterceptor接口。preHandle()在RequstMapping()方法执行前执行若返回值为true则放行该请求若返回值为false则该请求会被拦截。postHandle()在RequstMapping()方法执行后、返回响应结果给客户端前执行。afterCompletion()所有的请求响应结束后执行善后工作主要用于清理对象关闭资源。当preHandle()方法返回 true 时会将该方法放到专门的方法栈中等到对请求进行响应的所有工作完成之后才执行该方法。Component// 可以将拦截器添加到SpringMVC的IoC容器中方便在配置类中使用依赖注入获取拦截器Bean并指定拦截路径new也行就是不符合IoC理念publicclassMyInterceptorimplementsHandlerInterceptor{OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{//判断用户是否登陆过if(request.getSession().getAttribute(username)null){//用户未登录过则转发到登录界面并给出提示request.setAttribute(msg,您还没有登陆请先登录);request.getRequestDispatcher(/WEB-INF/jsp/login.jsp);returnfalse;//不放行}returntrue;//放行}OverridepublicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);}OverridepublicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{HandlerInterceptor.super.afterCompletion(request,response,handler,ex);}}三、指定拦截路径通过将配置类实现WebMvcConfigurer接口指定每个拦截器的拦截路径// 封装了springmvc.xml文件Configuration// 封装了xml文件中的文件头ComponentScan(org.example.controller)// 封装了了包扫描EnableWebMvc// 将前端传来的JSON数据自动转换成对象类型publicclassRequestMappingComponentScanimplementsWebMvcConfigurer{Autowired// 因为配置类本身也会被注册到IoC容器中所以可以使用依赖注入MyInterceptormyInterceptor;OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(myInterceptor).addPathPatterns(/url);}}等同于在springmvc.xml配置文件中进行如下配置!--注册拦截器--mvc:interceptorsmvc:interceptor!--映射要拦截的请求--mvc:mappingpath/**/!--映射要放行的请求--mvc:exclude-mappingpath/login/!--登录界面要放行--mvc:exclude-mappingpath/loginValid/!--判断登录是否成功的servlet要放行--!--配置实现拦截功能的类--beanclasscom.user.LoginInterceptor//mvc:interceptor/mvc:interceptors
SpringMVC 拦截器
目录一、拦截器执行时机二、拦截器实现方式三、指定拦截路径一、拦截器执行时机过滤器是在DispatcherServlet执行前后执行而拦截器是在RequstMapping()方法执行前后执行。所以过滤器可以处理所有请求但是拦截器只能处理DispatcherServlet的URL下的请求。二、拦截器实现方式继承HandlerInterceptorAdapter类。实现HandlerInterceptor接口。preHandle()在RequstMapping()方法执行前执行若返回值为true则放行该请求若返回值为false则该请求会被拦截。postHandle()在RequstMapping()方法执行后、返回响应结果给客户端前执行。afterCompletion()所有的请求响应结束后执行善后工作主要用于清理对象关闭资源。当preHandle()方法返回 true 时会将该方法放到专门的方法栈中等到对请求进行响应的所有工作完成之后才执行该方法。Component// 可以将拦截器添加到SpringMVC的IoC容器中方便在配置类中使用依赖注入获取拦截器Bean并指定拦截路径new也行就是不符合IoC理念publicclassMyInterceptorimplementsHandlerInterceptor{OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{//判断用户是否登陆过if(request.getSession().getAttribute(username)null){//用户未登录过则转发到登录界面并给出提示request.setAttribute(msg,您还没有登陆请先登录);request.getRequestDispatcher(/WEB-INF/jsp/login.jsp);returnfalse;//不放行}returntrue;//放行}OverridepublicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);}OverridepublicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{HandlerInterceptor.super.afterCompletion(request,response,handler,ex);}}三、指定拦截路径通过将配置类实现WebMvcConfigurer接口指定每个拦截器的拦截路径// 封装了springmvc.xml文件Configuration// 封装了xml文件中的文件头ComponentScan(org.example.controller)// 封装了了包扫描EnableWebMvc// 将前端传来的JSON数据自动转换成对象类型publicclassRequestMappingComponentScanimplementsWebMvcConfigurer{Autowired// 因为配置类本身也会被注册到IoC容器中所以可以使用依赖注入MyInterceptormyInterceptor;OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(myInterceptor).addPathPatterns(/url);}}等同于在springmvc.xml配置文件中进行如下配置!--注册拦截器--mvc:interceptorsmvc:interceptor!--映射要拦截的请求--mvc:mappingpath/**/!--映射要放行的请求--mvc:exclude-mappingpath/login/!--登录界面要放行--mvc:exclude-mappingpath/loginValid/!--判断登录是否成功的servlet要放行--!--配置实现拦截功能的类--beanclasscom.user.LoginInterceptor//mvc:interceptor/mvc:interceptors