SpringBoot拦截器的使用小结
总结一下SpringBoot下拦截器的使用,步骤很简单:
1.自定义自己的拦截类,拦截类需要继承HandlerInterceptor接口并实现这个接口的方法。
@Override
publicbooleanpreHandle(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto)throwsException{
//方法调用前执行
returntrue;//返回为false,拦截器拦截的方法不会调用
}
@Override
publicvoidpostHandle(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto,ModelAndViewmodelAndView)throwsException{
//方法执行结束后执行
}
@Override
publicvoidafterCompletion(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse,Objecto,Exceptione)throwsException{
//该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,这个方法的主要作用是用于清理资源的,
}
2.配置类需要继承WebMvcConfigurerAdapter类
@Autowired
privateLoginInterceptorloginInterceptor;//自己定义的拦截器类
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
registry.addInterceptor(loginInterceptor).addPathPatterns("拦截URL,可以不填默认全部请求拦截");
}
3.启动SpringBoot应用即可。
以上所述是小编给大家介绍的SpringBoot拦截器的使用小结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短