zoukankan      html  css  js  c++  java
  • 07springboot拦截器

    springboot拦截器

    2.0之前的拦截器(WebMvcConfigurerAdapter)

    • 使用注解 @Configuration配置拦截器
    • 继承WebMvcConfigurerAdapter
    • 重写addInterceptors 添加需要的拦截器地址
    @Configuration
    public class WebMvcConfigurer extends WebMvcConfigurerAdapter {//或者在主类上及继承
        //-------------------------------------------------------------------------------
    
        /**
        使用的时候才定义拦截器
        */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            /**
             * 拦截器按照顺序执行
             */
            registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/two/**")
                                                         .addPathPatterns("/one/**");//new TwoInterceptor()定义拦截器
            registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**");
    
            super.addInterceptors(registry);
        }
    
        //-------------------------------------------------------------------------------
        //或者先定义后使用:
        @Bean  //定义i=
        public LoginInterceptor loginInterceptor() {
            return new LoginInterceptor();
        }
        @Override //配置使用
        public void addInterceptors(InterceptorRegistry registry) {
            // addPathPatterns 用于添加拦截规则
            // excludePathPatterns 用户排除拦截
            // 登录拦截(登录,退出的去掉)
            registry.addInterceptor(loginInterceptor()).addPathPatterns("/ucareschedule/**")  //添加拦截
                    .excludePathPatterns("/ucareschedule/manager/login")
                    .excludePathPatterns("/index.html/");
        }
        //-------------------------------------------------------------------------------
    
    }

    2.0之后的拦截器

    主类设置继承类

    implements WebMvcConfigurer
    //拦截设置一致

    具体拦截类

    public class OneInterceptor implements HandlerInterceptor  {//或extends HandlerInterceptorAdapter
    
        /**
         * 在请求处理之前进行调用(Controller方法调用之前)
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
    
            System.out.println("被one拦截,放行...");
            return true;//true继续,false,拦截校验失败
    
        }
    
        /**
         * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
         */
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv)throws Exception {
    
        }
    
        /**
         * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
         * (主要是用于进行资源清理工作)
         */
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex)throws Exception {
    
        }
    }




  • 相关阅读:
    IG GROUP开源RESTdoclet项目
    Visual Studio 2012 Update 1抢先看
    使用 Windows Azure 移动服务将云添加到您的应用
    WMF 3.0 RTM(包含PowerShell 3.0 )业已发布
    Node.js 0.9.2 发布(非稳定版)
    vsftpd 3.0.1 正式版发布
    Piggydb 6.2 发布,个人知识库管理
    Apache Commons Codec 1.7 发布
    Notepad++ 6.1.8 正式版发布
    gWaei 3.6.0 发布,英日词典
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/bb627f17a003b9fe329cd37a89a84420.html
Copyright © 2011-2022 走看看