项目中配置过滤器,拦截器

一:使用过滤器的步骤

1:定义filter

package com.foxconn.sw.macaddress.common;

import javax.servlet.*;
import java.io.IOException;
import java.util.Date;

public class TestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("===开始过滤===");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long startTime = new Date().getTime();
        chain.doFilter(request, response);
        System.out.println("过滤请求耗时:" + (new Date().getTime() - startTime));
        System.out.println("===过滤完成===");
    }

    @Override
    public void destroy() {
        System.out.println("===销毁过滤===");
    }
}

2:使用过滤

package com.foxconn.sw.macaddress.common;

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.Collection;

/**
 * 将第三方filter使用到系统中
 */
@Configuration
public class WebConfig {
    @Bean
    public FilterRegistrationBean registrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        TestFilter testFilter = new TestFilter();
        filterRegistrationBean.setFilter(testFilter);
        Collection<String> urlList = new ArrayList<>();
        //过滤所有请求
        urlList.add("/*");
        filterRegistrationBean.setUrlPatterns(urlList);
        return filterRegistrationBean;
    }
}

缺点:不知道是哪个过滤的那个controller

二:使用拦截器的步骤

1:定义拦截器

package com.foxconn.sw.macaddress.common;

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 TestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("======preHandle====");
        //BeanName(controller的名字)
        System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
        //Bean Method Name(controller中被调用的方法的名字)
        System.out.println(((HandlerMethod) handler).getMethod().getClass().getName());
        request.setAttribute("startTime", new Date().getTime());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("======postHandle====");
        Long startTime = (Long) request.getAttribute("startTime");
        System.out.println("TestInterceptor 耗时:" + (new Date().getTime() - startTime));
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("======afterCompletion====");
        Long startTime = (Long) request.getAttribute("startTime");
        System.out.println("TestInterceptor 耗时:" + (new Date().getTime() - startTime));
        System.out.println("ex is " + ex);
    }
}

2:使用配置

package com.foxconn.sw.macaddress.common;

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.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 将第三方filter使用到系统中
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    TestInterceptor testInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(testInterceptor);
    }

    @Bean
    public FilterRegistrationBean registrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        TestFilter testFilter = new TestFilter();
        filterRegistrationBean.setFilter(testFilter);
        Collection<String> urlList = new ArrayList<>();
        //过滤所有请求
        urlList.add("/*");
        filterRegistrationBean.setUrlPatterns(urlList);
        return filterRegistrationBean;
    }
}

下方的@Bean可以不用加,直接使用拦截不使用过滤即可

缺点:定义拦截器只能获取到的是Object handler,是个Object,是个引用,不能获取到实际的对象的属性值,只能通过反射获取到controller类名和方法名

//BeanName(controller的名字)
System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
//Bean Method Name(controller中被调用的方法的名字)
System.out.println(((HandlerMethod) handler).getMethod().getClass().getName());

三:使用切片

使用

package com.foxconn.sw.macaddress.common;

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;

@Aspect
@Component
public class TestAspect {
    //什么时候执行(方法执行中),什么地方执行(execution代表的表达式MacaddressController里的所有方法)---整个是一个pointcut expressions注解
    @Around("execution(* com.foxconn.sw.macaddress.controller.MacaddressController.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("test aspect start");
        //controller下被调用的方法的请求参数
        Object[] args = pjp.getArgs();
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            System.out.println(arg);
        }
        long startTime = new Date().getTime();
        //controller下被调用的方法的返回值
        Object proceed = pjp.proceed();
        System.out.println("test aspect 耗时:" + (new Date().getTime() - startTime));
        System.out.println("test aspect end");
        return proceed;
    }
}

优点:可以获取拦截时的controller类名,方法名,方法参数的实际值,方法的返回值

3种拦截方式的执行顺序

从外到内,先执行过滤器,再执行拦截,当然如果有统一异常处理会先使用统一异常处理controllerAdvice再调用拦截,最后执行切片

当然如果发生了异常,捕获的顺序也是由内往外,先由controller抛出再被切片处理,依次往外

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值