有个Java Web开发经历的人,对拦截器想必都比较了解了,所以拦截器是什么这里就不多说了,下面将演示SpringBoot如何实现拦截器。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author wusy
* Company: xxxxxx科技有限公司
* Createtime : 2020/3/11 11:28
* Description : 自定义拦截器
*/
public class CustomHandlerInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(CustomHandlerInterceptor.class);
/**
* 需要忽略的地址
*/
private static String[] ignores = new String[]{};
/**
* 预处理回调方法,实现处理器的预处理
* 返回值:true表示继续流程;false表示流程中断,不会继续调用其他的拦截器或处理器
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
// 如果不是映射到方法直接通过
if (!(handler instanceof HandlerMethod)) {
return true;
}
//判断url是否需要拦截
if (this.ignoring(request.getRequestURI())) {
return true;
}
logger.info(request.getRequestURI() + " 预处理回调方法,实现处理器的预处理");
//执行业务逻辑如执行日志等等
return true;
}
/**
* 后处理回调方法,实现处理器(controller)的后处理,但在渲染视图之前
* 此时我们可以通过modelAndView对模型数据进行处理或对视图进行处理
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info(request.getRequestURI() + " 后处理回调方法,实现处理器(controller)的后处理,但在渲染视图之前");
}
/**
* 整个请求处理完毕回调方法,即在视图渲染完毕时回调,
* 如性能监控中我们可以在此记录结束时间并输出消耗时间,
* 还可以进行一些资源清理,类似于try-catch-finally中的finally,
* 但仅调用处理器执行链中
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info(request.getRequestURI() + " 整个请求处理完毕回调方法,即在视图渲染完毕时回调");
}
/**
* 判断url是否需要拦截
* @param uri
* @return
*/
private boolean ignoring(String uri) {
for (String string : ignores) {
if (uri.contains(string)) {
return true;
}
}
return false;
}
}
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.wusy.demo.interceptor.CustomHandlerInterceptor;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* @author wusy
* Company: xxxxxx科技有限公司
* Createtime : 2018年8月30日 下午6:34:44
* Description :
*/
@Configuration
public class WebConfigurerAdvice implements WebMvcConfigurer {
/**
* 添加管理拦截器
* @param registry 拦截器注册器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则,/**表示拦截所有请求
// excludePathPatterns 排除拦截
registry.addInterceptor(new CustomHandlerInterceptor()).addPathPatterns("/**");
}
}
接下来我们启动项目然后在浏览器中输入http://127.0.0.1:8787/api/swagger/demo(之前介绍过如何集成swagger时的测试接口),观察结果
从上图可以看出拦截器生效了。SpringBoot实现拦截器就是如此的简单。
来源:oschina
链接:https://my.oschina.net/u/2250817/blog/3192001