Spring Boot - filter order

Define the execution order of filters

1. The first filter

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class MyFirstFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化逻辑
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("执行 MyFirstFilter...");

        // 继续执行过滤器链
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 清理逻辑
    }
}

second filter

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class MySecondFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化逻辑,如果需要的话
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 你的自定义过滤器逻辑在这里
        System.out.println("执行 MySecondFilter...");

        // 继续执行过滤器链
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 清理逻辑,如果需要的话
    }
}

Configuration that defines the execution order of filters

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<MyFirstFilter> myFirstFilter() {
        FilterRegistrationBean<MyFirstFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyFirstFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<MySecondFilter> mySecondFilter() {
        FilterRegistrationBean<MySecondFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MySecondFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(2);
        return registrationBean;
    }
}

Define controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello filter!";
    }
}

Normal access: http://localhost:8080/hello
Insert image description here
Abnormal access: http://localhost:8080/HELLO
Insert image description here
Insert image description here
Viewable Path mapping is case-sensitive. If the path is incorrect, although 404 is issued, the filter chain will still be executed (/* in global circumstances).

Guess you like

Origin blog.csdn.net/qq_43116031/article/details/134477035