• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

springboot 拦截器

开发技术 开发技术 1周前 (07-19) 9次浏览

1、先自定义拦截器

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("登录拦截器");
        return false;
    }

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

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

2、使用

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
//        .addPathPatterns("/*") //拦截所有请求
        .addPathPatterns("") //拦截所有请求
//        .excludePathPatterns("/login");
        .excludePathPatterns("/login");
        super.addInterceptors(registry);//不拦截路径
    }
}

springboot 拦截器

 


程序员灯塔
转载请注明原文链接:springboot 拦截器
喜欢 (0)