请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

springboot 拦截器的配置

2023-07-13

编写拦截器

public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    }
}

对于前后端分离架构

  1. preHandle 会在方法执行前执行,按照拦截器顺序执行
  2. postHandle 会在方法return前执行,按照拦截器顺序逆序执行
  3. afterCompletion 方法return 之后执行 ,按照拦截器顺序逆序执行

拦截器注册

@Component
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	// order 拦截器的顺序,越小优先级越高
        registry.addInterceptor(new LoginInterceptor()).order(1);
    }
}

相关阅读

手机版|MSIPO技术圈 皖ICP备19022944号-2

Copyright © 2024, msipo.com

返回顶部