SpringFrameworkJava后端必备Java Web知识

Spring拦截器与Filter过滤器

2018-12-07  本文已影响34人  关捷

项目中,我们可以有两种方式,进行请求的过滤,一种是Filter过滤器,另一种就是Spring拦截器(HandlerInterceptor)。

它们都是起前置处理器的作用,能够在真正的业务逻辑执行之前进行某些前置处理,例如权限校验、登录校验、日志记录等等。

Filter过滤器

过滤器在我们刚接触web开发,学习Servlet的时候,就已经接触到了,应该非常的熟悉,有点需要注意,过滤器能够增强进出的逻辑,如下:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    //请求传递前处理
    chain.doFilter(request, response);
    //请求处理后处理
}

用起来比较简单,不做详细赘述,下面介绍几个比较重要的Filter。

CharacterEncodingFilter

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

因为web容器处理http请求,默认的编码格式是IOS8859-1CharacterEncodingFilter的作用就是设置请求体编码格式,用于请求体参数的字节转字符,也就是对Post请求的参数进行解码。

需要注意的是get请求或者URL上的参数是容器进行解码的,需要设置容器的URL解码的字符集。

Get请求与Post请求会有不同处理的原因在于,字符串解析是耗性能的,如果不需要使用,那么就不解析,也就不用消耗这部分性能。请求体通常比较大,只有在程序中需要的时候才会进行解码,而URL需要在传递给业务类之前就解码。

DelegatingFilterProxy

这个过滤器是用来代理自定义过滤器的,为什么需要代理?因为从过滤器的配置可以看出,过滤器是不能通过Spring依赖注入的,有时候很不方便,通过DelegatingFilterProxy进行代理,实际上就是通过ApplicationContext中获取filter对象

<filter>
    <filter-name>xxx</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>simpleFilter</param-value>
    </init-param>
</filter>
<filter-mapping>
<filter-name>xxx</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

HandlerInterceptor

作为SpringMVC提供的拦截器,HandlerInterceptor有以下三个方法:

在项目中使用,可以有两种方式,选择实现HandlerInterceptor接口或者继承HandlerInterceptorAdapter类,配置上有下面两种方式:

java-based方式配置

只需要继承WebMvcConfigurerAdapter,并重写addInterceptors()方法。

@Component
public class CustomerWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter{

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SimpleHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/path1")
    }
}

xml方式配置

使用的是MVC的命名空间标签

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/admin/**"/>
            <ref bean="simpleHandlerInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

Filter和HandlerInterceptor区别

相同点

Filter和HandlerInterceptor都可以说是过滤器,都可以在请求真正处理之前进行预处理操作,增强了实际业务处理的功能。

差异点

相较与Filter,HandlerInterceptor能够更加细粒度的拦截请求,从上面配置也可以看到,HandlerInterceptor能够配置多个拦截地址,并且能够排除也就是不过滤特殊请求。

关于请求链的传递,HandlerInterceptor通过布尔返回值判定是否继续传递,而Filter需要主动调用传递,也就是说ServletRequest和ServletResponse这两个对象在Filter传递时可以被替换。

从而可以认为HandlerInterceptor处理细粒度的拦截过滤,Filter处理粗粒度的,例如请求体中有自定义协议,需要预先解析然后替换ServletRequest或者解压缩等等操作。

上一篇下一篇

猜你喜欢

热点阅读