springboot注册拦截器所遇到的问题
问题1
springboot注册拦截器过滤器方法
注册拦截器:在启动类中注册bean
@EnableWebMvc @Configuration staticclassMvcConfigurerimplementsWebMvcConfigurer{ //在拦截器中需要使用这个bean,如果直接在拦截器中注入的话会失败,所以选择有参构造的方式传入 @Autowired CacheServicecacheService; @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ //指定拦截器类 registry.addInterceptor(newAuthorizationInterceptor(cacheService)) //指定该类拦截的url,过滤掉指定的url .addPathPatterns("/gw/**").excludePathPatterns("/**/query_token/**"); } } publicclassAuthorizationInterceptorextendsHandlerInterceptorAdapter{ //选择性的重写preHandlepostHandleafterCompletionafterConcurrentHandlingStarted方法 }
或者是
publicclassAuthorizationInterceptorimplementsHandlerInterceptor{ //重写preHandlepostHandleafterCompletion方法 defaultbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler) throwsException{ //请求开始执行之前执行改方法,返回true该请求猜能被继续执行下去,返回false的话,请求就直接结束 returntrue; } publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{ //该方法的执行时间是在处理器进行处理之后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的 //渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操作 } publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{ //该方法将在整个请求结束之后,也就是在DispatcherServlet渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。 } }
总结
以上所述是小编给大家介绍的springboot注册拦截器所遇到的问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!