Article Directory
[Shang Silicon Valley] SpringBoot2 Zero-Basic Introductory Tutorial - Lecturer: Lei Fengyang
Notes
The road is still going on, the dream is still waiting
1. HandlerInterceptor interface
The underlying interface of the interceptor
Location: org.springframework.web.servlet.HandlerInterceptor
public interface HandlerInterceptor {
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}
}
2. Interceptor use
2.1, custom interceptor
Writing an interceptor must implement the HandlerInterceptor interface
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
// 目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
log.info("preHandle拦截的请求路径是{}",requestURI);
//登录检查逻辑
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null){
//放行
return true;
}
//拦截住,未登录,跳转到登录页
request.setAttribute("msg","请先登录");
//re.sendRedirect("/");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
// 目标方法执行完成以后
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("postHandle执行{}",modelAndView);
}
// 页面渲染以后
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("afterCompletion执行异常{}",ex);
}
}
2.2, configure the interceptor
The interceptor is registered in the container (implements the addInterceptors method of the WebMvcConfigurer interface)
Specify interception rules [if all are intercepted, static resources will also be intercepted]
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
// 向容器中添加自定义拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 在拦截器注册中心中添加 LoginInterceptor 拦截器
registry.addInterceptor(new LoginInterceptor())
// 拦截的路径(所有请求都被拦截包括静态资源)
.addPathPatterns("/**")
// 放行的路径
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**");
}
}
3. Interceptor principle
- 1. According to the current request, find the HandlerExecutionChain . [Handlers that can handle requests and all interceptors of handlers]
- 2. Execute the preHandle methods of all interceptors in sequence first.
- 1. If the current interceptor prehandler returns true, execute the preHandle of the next interceptor.
- 2. If the current interceptor returns false, it will directly trigger the afterCompletion of all executed interceptors in reverse order.
- 3. If any interceptor fails to execute, return false, and jump out directly without executing the target method.
- 4. All interceptors return True and execute the target method.
- 5. Execute the postHandle methods of all interceptors in reverse order.
- 6. Any abnormality in the previous steps will directly trigger afterCompletion in reverse order.
- 7. After the page is successfully rendered, afterCompletion will also be triggered in reverse order.