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拦截器的使用小结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短