拦截器的基本认知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Interceptor1 implements HandlerInterceptor {

//执行时机:controller已经执行,modelAndview已经返回
//使用场景: 记录操作日志,记录登录用户的ip,时间等.
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("======Interceptor1=======afterCompletion========");
}

//执行时机:Controller方法已经执行,ModelAndView没有返回
//使用场景: 可以在此方法中设置全局的数据处理业务
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("======Interceptor1=======postHandle========");

}

//返回布尔值:如果返回true放行,返回false则被拦截住
//执行时机:controller方法没有被执行,ModelAndView没有被返回
//使用场景: 权限验证
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("======Interceptor1=======preHandle========");
return true;
}

}