zoukankan      html  css  js  c++  java
  • spring boot RESTFul API拦截 以及Filter和interceptor 、Aspect区别

           今天学习一下RESTFul api拦截 

             大概有三种方式

    一、通过Filter这个大家很熟悉了吧,这是java规范的一个过滤器,他会拦截请求。在springboot中一般有两种配置方式。

            这种过滤器拦截并不知道你用的是哪一个Controller处理也不知道你用哪一个方法处理。

      (1)第一种直接写类实现这个接口。代码如下这个要使用Component注解,当你你请求服务器的时候他会对每一个请求进行处理。

    package com.nbkj.webFilter;
    
    import org.springframework.stereotype.Component;
    
    import javax.servlet.*;
    import java.io.IOException;
    import java.util.Date;
    
    @Component
    public class TimerFilter 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 startTime = new Date().getTime();
            filterChain.doFilter(servletRequest, servletResponse);
            System.out.println("time filter:"+(new Date().getTime()-startTime));
            System.out.println("time filter finish");
        }
    
        @Override
        public void destroy() {
            System.out.println("Time filter destroy");
        }
    }

    (2)第二种可以在WebConfig中配置,这种配置方式为了使用第三方的Filter没有@Compont注解所以使用。代码如下

       

    package com.nbkj.config;
    
    import com.nbkj.webFilter.TimerFilter;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Web配置
     *
     * @author hsj
     * @Configuration 这个注解声明这个类是配置类
     * @create 2017-11-11 18:00
     **/
    
    @Configuration
    public class WebConfig {
        @Bean
        public FilterRegistrationBean timeFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            TimerFilter timerFilter = new TimerFilter();
            registrationBean.setFilter(timerFilter);
            List<String> urls = new ArrayList<>();
            urls.add("/*");
            registrationBean.setUrlPatterns(urls);
            return registrationBean;
        }
    }

    二、使用Interceptor这种事spring框架自己带的拦截器,代码如下 它会处理自己写的拦截器,也会拦截的拦截BasicErrorController

    可以拿到处理的Controller和拿到处理的方法 但是拿不到具体的请求参数。

    package com.nbkj.interceptor;
    
    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.persistence.Convert;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Date;
    
    /**
     * this is spring interceptor
     *
     * @author hsj
     * @create 2017-11-11 18:16
     **/
    
    @Component
    public class TimeInterceptor implements HandlerInterceptor {
    
        /**
         * 控制器方法处理之前
         *
         * @param httpServletRequest
         * @param httpServletResponse
         * @param handler
         * @return
         * @throws Exception
         */
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
            System.out.println("preHandle");
            System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
            System.out.println(((HandlerMethod) handler).getMethod().getName());
            httpServletRequest.setAttribute("startTime", new Date().getTime());
            return false;
        }
    
        /**
         * 控制器方法处理之后
         * 控制器方法调用不抛异常调用
         *
         * @param httpServletRequest
         * @param httpServletResponse
         * @param o
         * @param modelAndView
         * @throws Exception
         */
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("postHandle");
            Long startTime = (Long) httpServletRequest.getAttribute("startTime");
            System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime));
        }
    
        /**
         * 控制器方法抛不抛异常都会被调用
         *
         * @param httpServletRequest
         * @param httpServletResponse
         * @param o
         * @param e
         * @throws Exception
         */
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            System.out.println("afterCompletion");
            Long startTime = (Long) httpServletRequest.getAttribute("startTime");
            System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime));
            System.out.println("ex is" + e);
        }
    }
    package com.nbkj.config;
    
    import com.nbkj.interceptor.TimeInterceptor;
    import com.nbkj.webFilter.TimerFilter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Web配置
     *
     * @author hsj
     * @Configuration 这个注解声明这个类是配置类
     * @create 2017-11-11 18:00
     **/
    
    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        private TimeInterceptor timeInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(timeInterceptor);
        }
        
    }

        

     三、使用Aspect切片,代码如下

       使用环绕通知,切入要切入的类,当请求的时候回拦截下来,这样可以获取拦截的方法的参数

    package com.nbkj.aspect;
    
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * this is a acpect
     * 切入点
     * 在那些方法上起作用
     * 在什么时候起作用
     *
     * @author hsj
     * @create 2017-11-11 20:52
     **/
    @Aspect
    @Component
    public class TimeAspect {
        @Around("execution(* com.nbkj.controller.UserController.*(..))")
        public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            System.out.println("time aspect start");
            Object[] args = proceedingJoinPoint.getArgs();
            for (Object arg : args) {
                System.out.println(arg.getClass().getName());
                System.out.println("arg is " + arg);
            }
            long startTime = new Date().getTime();
            Object obj = proceedingJoinPoint.proceed();
            System.out.println("time aspect 耗时" + (new Date().getTime() - startTime));
            System.out.println("time aspect end");
            return obj;
        }
    }

    过滤器(Filter)         :可以拿到原始的http请求,但是拿不到你请求的控制器和请求控制器中的方法的信息。

    拦截器(Interceptor):可以拿到你请求的控制器和方法,却拿不到请求方法的参数。

    切片   (Aspect)       :  可以拿到方法的参数,但是却拿不到http请求和响应的对象

  • 相关阅读:
    作业三3
    作业三2(改过)
    第一章
    实验2
    第三章
    例2-11
    例2-10
    例2-8
    例2-9
    例2-7
  • 原文地址:https://www.cnblogs.com/bingshu/p/7819932.html
Copyright © 2011-2022 走看看