SpringBoot学习笔记五 | 配置拦截器

2018-03-26  本文已影响0人  殷俊杰

新建springboot工程

引入依赖

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前、request被响应之后、视图渲染之前以及request全部结束之后。我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执行。

新建MyInterceport实现HandlerInterceptor

在这里我们新建两个拦截器

package com.yjj.sbfirst.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("----------interceptor1请求处理前调用(Controller方法之前)-----------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("----------interceptor1请求处理后调用(Controller方法之后)-----------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("----------interceptor1整个请求处理后调用(DispatcherServlet渲染了视图之后执行)-----------");
    }
}

package com.yjj.sbfirst.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("----------interceptor2请求处理前调用(Controller方法之前)-----------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("----------interceptor2请求处理后调用(Controller方法之后)-----------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("----------interceptor2整个请求处理后调用(DispatcherServlet渲染了视图之后执行)-----------");
    }
}

配置拦截器

拦截器创建好之后需要我们进行配置,配置也很简单,只要继承WebMvcConfigurerAdapter重写addInterceptor方法就行了

package com.yjj.sbfirst.config;

import com.yjj.sbfirst.filter.MyInterceptor;
import com.yjj.sbfirst.filter.MyInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class HandlerInterceptorConfig extends WebMvcConfigurerAdapter{
    @Value("${interceptor.patters}")
    private String patters;
    //@Value("${interceptor.exclude}")
    private String exclude;

    @Autowired
    private MyInterceptor interceptor;
    @Autowired
    private MyInterceptor2 interceptor2;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(interceptor).addPathPatterns(patters);
        registry.addInterceptor(interceptor2).addPathPatterns(patters);
    }
}

addPathPatters方法是添加拦截路径,还有一个excludePathPatterns()方法可以排除路径。我这里拦截路径从配置文件application-dev.yml里读取。

interceptor.patters: /**

启动工程,随意访问一个Controller,控制台输出如下

----------interceptor1请求处理前调用(Controller方法之前)-----------
----------interceptor2请求处理前调用(Controller方法之前)-----------
----------interceptor2请求处理后调用(Controller方法之后)-----------
----------interceptor1请求处理后调用(Controller方法之后)-----------
----------interceptor2整个请求处理后调用(DispatcherServlet渲染了视图之后执行)-----------
----------interceptor1整个请求处理后调用(DispatcherServlet渲染了视图之后执行)-----------

注意多个拦截器的执行顺序。

最后强调一点:只有经过DispatcherServlet 的请求,才会走拦截器链,我们自定义的Servlet 请求是不会被拦截的,比如我们自定义的Servlet地址 是不会被拦截器拦截的。并且不管是属于哪个Servlet 只要复合过滤器的过滤规则,过滤器都会拦截。

最后说明下,我们上面用到的 WebMvcConfigurerAdapter 并非只是注册添加拦截器使用,其顾名思义是做Web配置用的,它还可以有很多其他作用,通过下面截图便可以大概了解,具体每个方法都是干什么用的,留给大家自己研究(其实都大同小异也很简单)。

参考文章:http://blog.csdn.net/catoop/article/details/50501696

上一篇下一篇

猜你喜欢

热点阅读