当前位置: 首页 > 后端技术 > Java

java-springboot添加自定义拦截器

时间:2023-04-01 13:33:55 Java

一天深夜,NicholasLisi突发奇想:将自己白天写的10个接口全部打印请求日志,日志内容也很简单,打印接口地址和请求即可参数没问题,但是打印请求日志的代码不想10个接口都写。我应该怎么办?Nicholas和李斯开始苦思冥想,想着springmvc的拦截器。1.自定义拦截器实现HandlerInteceptor接口。HandlerInterceptor接口中有三个方法preHandle:在进入Controller之前执行。返回值为布尔类型。如果返回false,则表示拦截请求,不再向下执行。如果返回true,则表示释放,程序继续向下执行(如果后面没有其他Interceptor,就会执行controller方法)。所以这个方法可以对请求进行判断,决定程序是否继续执行,或者对请求进行一些初始化操作和预处理在这个方法中被用来操作Controller处理过的ModelAndView对象,比如设置一个cookie返回给前端适用于一些资源清理@Component@Slf4jpublicclassLogRequestUriInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{StringrequestURI=request.getRequestURI();MapparameterMap=request.getParameterMap();log.info("==Receivedrequest,requestURI:{},queryString:{}==",requestURI,JsonUtil.toJsonDisableHtmlEscaping(parameterMap));返回真;}}2.添加拦截器(有两种方式,这里选择第二种)1.extendsWebMvcConfigurerAdapter(spring5.0开始废弃)WebMvcConfigurerAdapter@ConfigurationpublicclassTestMvcConfigureextendsWebMvcConfigurerAdapter{@ResourceprivateLogRequestUriInterceptorlogRequestUriInterceptor;@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(logRequestUriInterceptor);}}二。实现WebMvcConfigurer@ConfigurationpublicclassWebMvcConfigure实现WebMvcConfigurer{@ResourceprivateLogRequestUriInterceptorlogRequestUriInterceptor;@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(logRequestUriInterceptor);}3。启动项目并测试请求。如果需要添加多个拦截器,当然再定义一个拦截器,添加进去自定义第二个拦截器info("==自定义拦截器2,uri:{},queryString:{}==",request.getRequestURI(),JsonUtil.toJsonDisableHtmlEscaping(request.getParameterMap()));返回真;}}添加第二个拦截器@ConfigurationpublicclassWebMvcConfigureimplementsWebMvcConfigurer{@ResourceprivateLogRequestUriInterceptorlogRequestUriInterceptor;@ResourceprivateRecordLogInterceptorrecordLogInterceptor;@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(logRequestUriInterceptor);registry.addInterceptor(recordLogInterceptor);}}启动项目,测试请求从打印日志可以看出,此时拦截器生效的顺序和添加的顺序是一样的。又是一个暴风雨的夜晚,尼古拉斯。老王隔壁。王五添加自定义拦截器时,只需要定义拦截器即可。无需手动添加转换并开始创建具有排序方法的新抽象公共拦截器。publicabstractclassAbstractHandlerInterceptorimplementsHandlerInterceptor{/***通过该方法可以自动按顺序添加拦截器*@return*/abstractIntegersort();}拦截器1修改为继承@Component@Slf4jpublicclassLogRequestUriInterceptorextendsAbstractHandlerInterceptor{@覆盖publicbooleanpreHandle(HttpServletRequest请求,HttpServletResponse响应,对象处理程序)抛出异常{StringrequestURI=request.getRequestURI();MapparameterMap=request.getParameterMap();log.info("==自定义拦截器1,requestURI:{},queryString:{}==",requestURI,JsonUtil.toJsonDisableHtmlEscaping(parameterMap));返回真;}@Override整数排序(){返回1;request,HttpServletResponseresponse,Objecthandler)throwsException{log.info("==自定义拦截器2,uri:{},queryString:{}==",request.getRequestURI(),JsonUtil.toJsonDisableHtmlEscaping(request.getParameterMap()));返回真;}@Override整数排序(){返回2;}}添加拦截器逻辑调整@ConfigurationpublicclassWebMvcConfigureimplementsWebMvcConfigurer{@ResourceprivateApplicationContextapplicationContext;@覆盖publicvoidaddInterceptors(InterceptorRegistryregistry){MapinterceptorMap=applicationContext.getBeansOfType(AbstractHandlerInterceptor.class);SettreeSet=newTreeSet((o1,o2)->o1.sort()-o1.sort());treeSet.addAll(interceptorMap.values());for(AbstractHandlerInterceptor拦截器:treeSet){registry.addInterceptor(拦截器);}}}启动项目,测试请求未完成等待...