亲宝软件园·资讯

展开

Java拦截器 过滤器

zhangzengxiu 人气:0

流程图

拦截器vs过滤器

拦截器是SpringMVC的技术

过滤器的Servlet的技术

先过过滤器,过滤器过完才到DispatcherServlet;

拦截器归属于SpringMVC,只可能拦SpringMVC的东西;

拦截器说白了就是为了增强,可以在请求前进行增强,也可以在请求后进行增强,但是不一定同时在前后都增强。

区别

SpringMVC技术架构图

项目Demo

项目地址

依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

Interceptor拦截器

package com.test.filtertest.inter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 拦截器
 *
 * @author: zhangzengxiu
 * @createDate: 2022/4/22
 */
@Component
public class TestInter1 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("TestInter1 preHandle...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("TestInter1 postHandle...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("TestInter1 afterCompletion...");
    }
}

Interceptor拦截器要注册

package com.test.filtertest.config;

import com.test.filtertest.inter.TestInter1;
import com.test.filtertest.inter.TestInter2;
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;

/**
 * @author zhangzengxiu
 * @date 2022/4/22
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TestInter1 testInter1;

    @Autowired
    private TestInter2 testInter2;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //默认/** 拦截所有请求
        registry.addInterceptor(testInter1).addPathPatterns("/**");
        registry.addInterceptor(testInter2);
    }
}

拦截顺序

多拦截器拦截顺序取决于registry的注册顺序

Filter过滤器

1、多Filter不指定过滤顺序

package com.test.filtertest.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author zhangzengxiu
 * @date 2022/4/22
 */
@WebFilter(filterName = "TestFilter1", urlPatterns = "/*")
//@Order(2) //filter这样定义拦截顺序无效,还是会按照类名去拦截
public class TestFilter1 implements Filter {

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

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("TestFilter1 filter before ...");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("TestFilter1 filter after ...");
    }

    @Override
    public void destroy() {

    }
}

加了@WebFilter注解的过滤器要生效,需要在启动类上加一个注解@ServletComponentScan

2、多Filter指定过滤顺序

如果要设置过滤顺序的话,在Filter上添加@Order 不会生效,还是会默认根据Filter过滤器的类名去进行过滤。

如果想要指定拦截顺序,需要添加一个配置类,通过设置Order的值去设置过滤顺序,Order数值越小优先级越高

package com.test.config;

import com.test.filter.TestFilter1;
import com.test.filter.TestFilter2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * filter过滤器配置
 *
 * @author: zhangzengxiu
 * @createDate: 2022/4/24
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean testFilter1() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        Filter testFilter1 = new TestFilter1();
        filterRegistrationBean.setFilter(testFilter1);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("testFilter1");
        filterRegistrationBean.setOrder(2);//指定过滤顺序,数值越小优先级越高
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean testFilter2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        Filter testFilter2 = new TestFilter2();
        filterRegistrationBean.setFilter(testFilter2);
        filterRegistrationBean.setName("testFilter2");
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(1);//指定过滤顺序,数值越小优先级越高
        return filterRegistrationBean;
    }
}

1、同时原本Filter过滤器上的@WebFilter(filterName = “TestFilter1”, urlPatterns = “/*”)

这个注解最好去掉,否则bean重复会报错,要么bean名称不要重复,换个名称。但是不去掉的话@ServletComponentScan也要被保留,最好统一格式,只使用config配置。

2、启动类上的@ServletComponentScan 注解也不需要了,可以去掉了,因为不用扫描@WebFilter注解了。但是如果@WebFilter还保留了,那么就不能去掉了。不然加了@WebFilter的过滤器就不会生效了,因为没扫到。

加载全部内容

相关教程
猜你喜欢
用户评论