美文网首页java后台
SpringBoot系列—自定义拦截器(九)

SpringBoot系列—自定义拦截器(九)

作者: 海晨忆 | 来源:发表于2019-09-26 14:15 被阅读0次

    个人博客:haichenyi.com。感谢关注

      拦截器拦截请求做额外的处理。

    举个栗子:登录拦截器,拦截所有的请求,必须登录之后才能访问。

    package com.haichenyi.springbootbill.interceptors;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 登录拦截器
     */
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            Object user = request.getSession().getAttribute("user");
            if (user != null) {
                return true;
            }
            request.setAttribute("msg", "您还没有登录,请先登录!");
            request.getRequestDispatcher("/index.html").forward(request, response);
            return false;
        }
    }
    
    

      新建LoginInterceptor实现HandlerInterceptor接口。这个接口有三个方法,pre,post,after, 看这个名字就可以想到这三个方法是怎么调用的,发送请求前,发送过程中,发送请求成功之后,所以,根据自己的需求,实现对应的方法即可。

      我这里是登录拦截器,所以,在发送请求之前就要拦截,走自己的逻辑,如果,没有登陆过,就跳转登录界面,所以,我这里就实现了发送请求之前的回调,即preHandle方法。

      然后,在你的SpringMvcConfiguration里面加上拦截器即可。

    package com.haichenyi.springbootbill.config;
    
    import com.haichenyi.springbootbill.component.MyLocalResolver;
    import com.haichenyi.springbootbill.interceptors.LoginInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.LocaleResolver;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class MySpringMvcConfig {
    
        @Bean
        public WebMvcConfigurer webMvcConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/").setViewName("main/login");
                    registry.addViewController("/index.html").setViewName("main/login");
                    registry.addViewController("/main/index.html").setViewName("main/index");
                }
    
                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    registry.addInterceptor(new LoginInterceptor())
                            .addPathPatterns("/**")
                            .excludePathPatterns("/", "/index.html", "/login")
                            .excludePathPatterns("/css/*","/img/*","/images/*","/js/*");
                }
            };
        }
    
        @Bean
        public LocaleResolver localeResolver() {
            return new MyLocalResolver();
        }
    }
    
    

      实现addInterceptors方法,添加拦截器,然后添加了 addPathPatterns 拦截所有带这个参数的请求。接着,又添加了 excludePathPatterns 不拦截所有带这个参数的请求。

    相关文章

      网友评论

        本文标题:SpringBoot系列—自定义拦截器(九)

        本文链接:https://www.haomeiwen.com/subject/jczcyctx.html