上面这两种拦截请求的实现是基于JavaEE提供的Filter接口实现的,缺点在于,该拦截器实际上是一个过滤器,执行代码的方法doFilter只提供了request,response等参数,当请求进入被过滤器拦截的时候,我们并不知道这个请求是由哪个控制器的哪个方法来执行的 。
三:使用springMVC提供的拦截器,实现org.springframework.web.servlet.HandlerInterceptor接口:
创建自定义的拦截器:
import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Date;@Componentpublic class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {System.out.println("preHandler");System.out.println(((HandlerMethod) handler).getBean().getClass().getName());System.out.println(((HandlerMethod) handler).getMethod().getName());httpServletRequest.setAttribute("start", new Date().getTime());return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("postHandler");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("afterCompletion");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));System.out.println("ex is:"+e);}}
创建配置类:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate MyInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");}}
此种方式的拦截器当中我们能够获取拦截的请求对应的类和方法的相关信息,缺点在于该handler对象无法获取具体执行方法的参数信息 。
四:利用Spring的切面(AOP)实现拦截器:
引入jar包:
!----dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependency
创建切片类:
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import java.util.Date;@Aspect@Componentpublic class TimeAspect {@Around("execution(* com.qinker.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable {System.out.println("time aspect start");long start = new Date().getTime();Object[] args = point.getArgs();for (Object obj : args) {System.out.println("arg is:"+obj);}Object obj = point.proceed();//具体方法的返回值System.out.println("aspect 耗时:"+(new Date().getTime()-start));System.out.println("time aspect end");return obj;}}
aspectj基于AOP实现的拦截器功能十分强大,具体详解请参考spring网站的文档 。
java filter 过滤器中文乱码 怎么解决?。?/h2>1、首先编写一个Java类的filter代码 。操作步骤:
(1)在myeclipse中新建一个java类,
(2)单击“Add”按钮,在弹出来的对话框中“选择接口”文本框中输入Filter,
推荐阅读
- 跨境电商erp系统数据库,跨境电商erp业务流程图
- 用jquery制作鼠标表格,jquery代码实现鼠标点击
- html代码起始结束标记,html的结束标记
- linux命令大全mv linux命令大全手册离线版
- 怎么加星火公众号群组链接,怎么加入星火社群
- 模拟羽翼飞行游戏,翼龙飞行模拟器
- thinkphpruntime目录,php think run p 8080目录
- 怎么卸载mysql安装包 如何卸载已经安装的mysql
- asp.net程序设计教程c版实训,aspnet程序设计案例教程课后答案