您的位置:首页 > 编程语言 > Java开发

SpringBoot学习之路:11.Spring Boot中添加Interceptor应用

2017-07-07 00:00 274 查看
摘要: SpringBoot学习之路之Spring Boot中添加Interceptor应用

前面说了SpringBoot中使用servlet、过滤器(filter)、监听器(Listener),还有在web应用中使用比较多的是拦截器Interceptor,像之前的Strutrs2框架实现的核心就是一套套的拦截器,那么springBoot项目中怎么使用自定义的拦截器呢?

1.编写自定义拦截器实现HandlerInterceptor接口

自定义拦截器1:

package com.maxbill.core.webbox.interceptor;

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

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

/**
* @功能 自定义拦截器
* @作者 zuoshuai(MaxBill)
* @日期 2017/7/6
* @时间 15:42
* @备注 只有经过DispatcherServlet的请求,才会走拦截器链,
*       我们自定义的Servlet请求是不会被拦截的;Servlet只要符合过滤器的过滤规则,过滤器都会过滤。
*/
public class Interceptor01 implements HandlerInterceptor {

/**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println(">>>Interceptor01>>>>>>>在请求处理之前进行调用");
return true;// 只有返回true才会继续向下执行,返回false取消当前请求
}

/**
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println(">>>Interceptor01>>>>>>>请求处理之后进行调用");
}

/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
*(主要是用于进行资源清理工作)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
System.out.println(">>>Interceptor01>>>>>>>在整个请求结束之后被调用");
}

}

自定义拦截器2:

package com.maxbill.core.webbox.interceptor;

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

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

/**
* @功能 自定义拦截器
* @作者 zuoshuai(MaxBill)
* @日期 2017/7/6
* @时间 15:42
* @备注 只有经过DispatcherServlet的请求,才会走拦截器链,我们自定义的Servlet请求是不会被拦截的;Servlet只要符合过滤器的过滤规则,过滤器都会过滤。
*/
public class Interceptor02 implements HandlerInterceptor {

/**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(">>>Interceptor02>>>>>>>在请求处理之前进行调用");
return true;// 只有返回true才会继续向下执行,返回false取消当前请求
}

/**
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(">>>Interceptor02>>>>>>>请求处理之后进行调用");
}

/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println(">>>Interceptor02>>>>>>>在整个请求结束之后被调用");
}

}


2.编写WebMvcConfig配置类继承WebMvcConfigurerAdapter

package com.maxbill.core.config.webapp;

import com.maxbill.core.webbox.interceptor.Interceptor01;
import com.maxbill.core.webbox.interceptor.Interceptor02;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
* @功能
* @作者 zuoshuai(MaxBill)
* @日期 2017/7/6
* @时间 15:40
* @备注 WebMvcConfig
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

/**
* 拦截器注册
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new Interceptor01()).addPathPatterns("/**");
registry.addInterceptor(new Interceptor02()).addPathPatterns("/**");
super.addInterceptors(registry);
}

}


3.启动项目查看拦截器作用

打印日志如下:

>>>Interceptor01>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>请求处理之后进行调用
>>>Interceptor01>>>>>>>请求处理之后进行调用
>>>Interceptor02>>>>>>>在整个请求结束之后被调用
>>>Interceptor01>>>>>>>在整个请求结束之后被调用

4.拦截器疑点说明(说明)

启动项目后,请求昨天编写的servlet,结果发现并没有走我们的拦截器,而是走了我们昨天的过滤器,于是可以得出结论:只有经过DispatcherServlet分发的请求,才会走拦截器链;我们自定义的Servlet请求是不会被拦截的;我们的Servlet只要符合过滤器的过滤规则,过滤器都会过滤。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot Interceptor