Java实现拦截HTTP请求的几种方式

在Java的服务端开发当中,拦截器是很常见的业务场景,这里对Java开发当中几种常见的拦截器的实现方式进行记录和分析。
一:实现javax.servlet.Filter接口(使用过滤器方式拦截请求)

import org.springframework.stereotype.Component;
 
import javax.servlet.*;
import java.io.IOException;
import java.util.Date;
 
@Component
public class TimeInterceptor implements Filter {
     
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
     
        System.out.println("time filter init");
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
     
        System.out.println("time filter start");
        long start = new Date().getTime();
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("time filter 耗时:"+(new Date().getTime()-start));
        System.out.println("time filter finish");
    }
 
    @Override
    public void destroy() {
     
        System.out.println("time filter destroy");
    }
}

三:使用springMVC提供的拦截器,实现org.springframework.web.servlet.HandlerInterceptor接口:

创建自定义的拦截器:

import org.springframework.stereotype.Component;
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;
import java.util.Date;
 
@Component
public class MyInterceptor implements HandlerInterceptor {
     
 
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
     
        System.out.println("preHandler");
        System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
        System.out.println(((HandlerMethod) handler).getMethod().getName());
        httpServletRequest.setAttribute("start", new Date().getTime());
        return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
     
        System.out.println("postHandler");
        Long start = (Long) httpServletRequest.getAttribute("start");
        System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));
    }
 
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
     
        System.out.println("afterCompletion");
        Long start = (Long) httpServletRequest.getAttribute("start");
        System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));
        System.out.println("ex is:"+e);
    }
}

创建配置类:

import org.springframework.beans.factory.annotation.Autowired;
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 WebConfig extends WebMvcConfigurerAdapter {
     
 
    @Autowired
    private MyInterceptor interceptor;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     
        registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");
    }
}

原文链接:
https://blog.csdn.net/lory_li/article/details/80835410?utm_source=blogxgwz7

你可能感兴趣的:(计算机,java)