首页 > 编程语言 > 通过实例解析java过滤器和拦截器的区别
2020
09-24

通过实例解析java过滤器和拦截器的区别

区别

1.使用范围和规范不同

filter是servlet规范规定的,只能用在web程序中.

拦截器即可以用在web程序中, 也可以用于application, swing程序中, 是Spring容器内的, 是Spring框架支持的

2.触发时机不同

顺序: Filter-->Servlet-->Interceptor-->Controller

  • 过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的。请求结束返回也是,是在servlet处理完后,返回给前端之前过滤器处理。
  • 拦截器是方法到达Controller层之前生效的

3.过滤器的实现基于回调函数。而拦截器(代理模式)的实现基于反射,代理分静态代理和动态代理,动态代理是拦截器的简单实现。

何时使用拦截器?何时使用过滤器?

  • 如果是非spring项目,那么拦截器不能用,只能使用过滤器。
  • 如果是处理controller前后,既可以使用拦截器也可以使用过滤器。
  • 如果是处理dispaterServlet前后,只能使用过滤器。

4.在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次。

5.拦截器可以访问action上下文、值栈里的对象,而过滤器不能访问。

6.拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用。

7.拦截器可以获取IOC容器中的各个bean,而过滤器就不行,在拦截器里注入一个service,可以调用业务逻辑。

SpringBoot使用过滤器

两种方式:

1、使用spring boot提供的FilterRegistrationBean注册Filter

2、使用原生servlet注解定义Filter

两种方式的本质都是一样的,都是去FilterRegistrationBean注册自定义Filter

封装Filter

package com.theeternity.common.baseFilter;
import javax.servlet.Filter;
/**
 * @program: ApiBoot
 * @description: 封装Filter
 * @author: TheEternity Zhang
 * @create: 2019-02-17 13:08
 */
public interface MappingFilter extends Filter {
  String[] addUrlPatterns();
  int order();
}

自定义Filter

package com.theeternity.beans.filterConfig;

import com.theeternity.common.baseFilter.MappingFilter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import java.io.IOException;

/**
 * @program: ApiBoot
 * @description: 权限过滤器
 * @author: TheEternity Zhang
 * @create: 2019-02-17 13:14
 */
public class AuthFilter implements MappingFilter {

  @Override
  public String[] addUrlPatterns() {
    return new String[]{"/*"};
  }

  @Override
  public int order() {
    return 0;
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    filterChain.doFilter(servletRequest,servletResponse);
  }

  @Override
  public void destroy() {
  }
}

注册过滤器

package com.theeternity.beans.filterConfig;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @program: ApiBoot
 * @description: 注册过滤器
 * @author: TheEternity Zhang
 * @create: 2019-02-17 13:10
 */
@Configuration
public class FilterConfig {

  @Bean
  public FilterRegistrationBean registFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    AuthFilter authFilter=new AuthFilter();
    registration.setFilter(authFilter);
    registration.addUrlPatterns(authFilter.addUrlPatterns());
    registration.setOrder(authFilter.order());
    registration.setName("AuthFilter");
    return registration;
  }
}

SpringBoot使用拦截器

封装Interceptor

package com.theeternity.common.baseInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
/**
 * @program: ApiBoot
 * @description: 封装Interceptor
 * @author: TheEternity Zhang
 * @create: 2019-02-15 17:49
 */
public interface MappingInterceptor extends HandlerInterceptor {
  String[] addPathPatterns();

  String[] excludePathPatterns();

  int order();
}

自定义Interceptor

package com.theeternity.beans.interceptorConfig;

import com.theeternity.common.baseInterceptor.MappingInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: BoxApi
 * @description: 跨域拦截器
 * @author: tonyzhang
 * @create: 2018-12-21 14:44
 */
@Component
public class CrossOriginInterceptor implements MappingInterceptor {

  @Override
  public String[] addPathPatterns() {
    return new String[]{"/**"};
  }

  @Override
  public String[] excludePathPatterns() {
    return new String[0];
  }

  @Override
  public int order() {
    return 0;
  }

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    logger.info("允许的头信息"+request.getHeader("Origin"));
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "*");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    //是否允许浏览器携带用户身份信息(cookie)
    response.setHeader("Access-Control-Allow-Credentials","true");
    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 {
  }
}

注册Interceptor

package com.theeternity.beans.interceptorConfig;

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

/**
 * @program: ApiBoot
 * @description: 拦截器注册
 * @author: TheEternity Zhang
 * @create: 2019-02-15 17:55
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

  @Autowired
  private CrossOriginInterceptor crossOriginInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(crossOriginInterceptor).addPathPatterns(crossOriginInterceptor.addPathPatterns());

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧