總結(jié)一下SpringBoot下攔截器的使用,步驟很簡單:
1.自定義自己的攔截類,攔截類需要繼承HandlerInterceptor接口并實現(xiàn)這個接口的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { //方法調(diào)用前執(zhí)行 return true ; //返回為false,攔截器攔截的方法不會調(diào)用 } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { //方法執(zhí)行結(jié)束后執(zhí)行 } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { //該方法將在整個請求完成之后,也就是DispatcherServlet渲染了視圖執(zhí)行, 這個方法的主要作用是用于清理資源的, } |
2.配置類需要繼承WebMvcConfigurerAdapter類
1
2
3
4
5
6
|
@Autowired private LoginInterceptor loginInterceptor; //自己定義的攔截器類 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor).addPathPatterns( "攔截URL,可以不填默認全部請求攔截" ); } |
3.啟動SpringBoot應(yīng)用即可。
以上所述是小編給大家介紹的SpringBoot攔截器的使用小結(jié),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
原文鏈接:http://www.cnblogs.com/lfjjava/p/6093388.html