本篇文章主要介绍 Springboot 中 Filter 和 Interceptor 的代码示例,关于 Filter、Interceptor 的执行原理,请参见《Springboot工作原理》
Filter
注册 Filter 链
@Configuration
public class HttpServletFilterConfig {
@Bean
public FilterRegistrationBean httpServletExeTimeFilter() {
return generateRegistrationBean(new HttpServletExeTimeFilter(), 1, "/*");
}
@Bean
public FilterRegistrationBean httpServletDiscardFilter() {
return generateRegistrationBean(new HttpServletDiscardFilter(), 2, "/*");
}
@Bean
public FilterRegistrationBean httpServletGzipFilter() {
return generateRegistrationBean(new HttpServletGzipFilter(), 3, "/*");
}
@Bean
public FilterRegistrationBean httpServletValidateFilter() {
return generateRegistrationBean(new HttpServletValidateFilter(), 4, "/*");
}
private static <T extends AbstractHttpServletFilter> FilterRegistrationBean generateRegistrationBean(T filter, int order, String urlPattern) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(order);
filterRegistrationBean.setFilter(filter);
List<String> urlPatterns = new ArrayList<>();
urlPatterns.add(urlPattern);
filterRegistrationBean.setUrlPatterns(urlPatterns);
return filterRegistrationBean;
}
}
AbstractHttpServletFilter
public abstract class AbstractHttpServletFilter implements Filter {
@Override
public abstract void init(FilterConfig filterConfig) throws ServletException;
@Override
public abstract void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
@Override
public abstract void destroy();
}
响应时间统计
@Slf4j
@WebFilter(filterName = "httpServletExeTimeFilter", urlPatterns = "/")
public class HttpServletExeTimeFilter extends AbstractHttpServletFilter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
long beginTime = System.currentTimeMillis();
AppStateMonitor.RECV_COUNTER.inc();
chain.doFilter(request, response);
long endTime = System.currentTimeMillis();
long consumeTime = endTime - beginTime;
if (consumeTime > AppCoreConstant.CONSUME_TIME_OUT) {
AppStateMonitor.TOUT_CLI_COUNTER.inc();
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
LogUtil.warn(log, "{0} consume {1} millis", httpServletRequest.getRequestURI(), consumeTime);
}
}
@Override
public void destroy() {}
}
数据流转换
根据 Request header 的 Content-Encoding 将I/O流转成 Gzip 流
@WebFilter(filterName = "httpServletGzipFilter", urlPatterns = "/")
public class HttpServletGzipFilter extends AbstractHttpServletFilter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request), response);
}
@Override
public void destroy() {}
}
@Slf4j
class HttpServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper {
private HttpServletRequest request;
public HttpServletRequestWrapper(HttpServletRequest request) {
super(request);
this.request = request;
}
/**
* 根据 request header 的 Content-Encoding 判断是否启用 gzip 解压数据流
*/
@Override
public ServletInputStream getInputStream() throws IOException {
ServletInputStream stream = request.getInputStream();
String contentEncoding = request.getHeader("Content-Encoding");
if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {
try {
final GZIPInputStream gzipInputStream = new GZIPInputStream(stream);
ServletInputStream newStream = new ServletInputStream() {
@Override
public int read() throws IOException {
return gzipInputStream.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {}
};
return newStream;
} catch (Exception e) {
AppStateMonitor.ERROR_COUNTER.inc();
throw new ResponseException(ResponseStatus.BAD_REQUEST, e, "uncompress content fail! user-agent:" + request.getHeader("user-agent") + ", Content-Encoding:" + contentEncoding);
}
}
return stream;
}
}
请求过滤
@Slf4j
@WebFilter(filterName = "httpServletValidateFilter", urlPatterns = "/")
public class HttpServletValidateFilter extends AbstractHttpServletFilter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!validateProcess(request);) {
response.setContentType(AppCoreConstant.DEFAULT_CONTENT_TYPE);
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(new ServletFilterDto(ResponseStatus.BAD_REQUEST.getEc(), ResponseStatus.BAD_REQUEST.getEm()).toString().getBytes());
return;
}
AppStateMonitor.PROC_COUNTER.inc();
chain.doFilter(request, response);
}
@Override
public void destroy() { }
}
Interceptor
注册拦截器
@Slf4j
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.pesen.roc.web.**.**")
@PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8")
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private ExeTimeIterceptor exeTimeIterceptor;
/**
* 注册拦截器
*
* @param registry registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(exeTimeIterceptor)
.addPathPatterns("/**");
}
}
响应时间统计
@Slf4j
@Component
public class ExeTimeIterceptor implements HandlerInterceptor {
private NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<>("StopWatch-StartTime");
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
startTimeThreadLocal.set(System.currentTimeMillis());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long endTime = System.currentTimeMillis();
long beginTime = startTimeThreadLocal.get();
long consumeTime = endTime - beginTime;
if (consumeTime > AppCoreConstant.CONSUME_TIME_OUT) {
LogUtil.warn(log, "{0} consume {1} millis", request.getRequestURI(), consumeTime);
}
}
}