欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot之过滤器

程序员文章站 2022-05-23 10:13:04
...

过滤器

过滤器可以在前端拦截所有用户的请求。

过滤器的应用

  • 对web服务器所有资源的管理
  • 对url级别的权限进行控制
  • 过滤数据
  • 记录日志

自定义Filter的实现方式

  • @WebFilter 不能设置过滤器的优先级
  • FilterRegistrationBean

自定义Filter步骤

  1. 实现Filter接口
  2. 添加@Configuration注解,将自定义的Filter加入过滤器链

新建MyFilter

package com.chen.springbootfilter.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("this is MyFilter,url :"+request.getRequestURI());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("===init===");
    }

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

新建配置类

package com.chen.springbootfilter.config;

import com.chen.springbootfilter.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {

    @Bean
    public FilterRegistrationBean testFilterRegistration(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter");
        registration.setOrder(10);
        return registration;
    }
}

新建Controller

package com.chen.springbootfilter.controller;

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

@RestController
public class HelloController {

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

代码结构

SpringBoot之过滤器

运行结果

SpringBoot之过滤器

优先级测试

新建MyFilter2,代码和MyFilter一致。
在配置类中设置优先级。

package com.chen.springbootfilter.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class MyFilter2 implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("this is MyFilter 2,url :"+request.getRequestURI());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("===init===");
    }

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

package com.chen.springbootfilter.config;

import com.chen.springbootfilter.filter.MyFilter;
import com.chen.springbootfilter.filter.MyFilter2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter");
        registration.setOrder(10);
        return registration;
    }

    @Bean
    public FilterRegistrationBean test2FilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter2());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter2");
        registration.setOrder(1);
        return registration;
    }

}

SpringBoot之过滤器
总结:过滤器器 MyFilter 2 因为 Order 值设置得低,会优先被执⾏。