一张思维导图带你学会SpringBoot自定义Filter

129 阅读3分钟

🧑‍💻作者名称:DaenCode 🎤作者简介:啥技术都喜欢捣鼓捣鼓,喜欢分享技术、经验、生活。 😎人生感悟:尝尽人生百味,方知世间冷暖。 📖所属专栏:SpringBoot实战


系列文章目录

标题
一文带你学会使用SpringBoot+Avue实现短信通知功能(含重要文件代码)
一张思维导图带你学会Springboot创建全局异常、自定义异常
一张思维导图带你打通SpringBoot自定义拦截器的思路
28个SpringBoot项目中常用注解,日常开发、求职面试不再懵圈
一张思维导图带你学会SpringBoot、Vue前后端分离项目线上部署
一张流程图带你学会SpringBoot结合JWT实现登录功能
一张思维导图带你学会使用SpringBoot中的Schedule定时发送邮件
一张思维导图带你学会使用SpringBoot异步任务实现下单校验库存
一张思维导图带你学会SpringBoot使用AOP实现日志管理功能
一张图带你学会入门级别的SpringBoot实现文件上传、下载功能

在这里插入图片描述


@[toc]

思维导图

在这里插入图片描述

🌟前言

在开发中,都离不开自定义过滤器的使用。本文的主要内容是通过SpringBoot实现自定义过滤器。

🌟知识准备

过滤器:类似于人们进入景点或者电影院的检票厅。 种类:如下图所示,位于org.springframework.boot.web.servlet.filter中。 在这里插入图片描述 优先级:属性值越低则代表属性值越高。可以查看思维导图。

方式一:使用FilterRegistrationBean配置类进行注册。类似于自定义拦截器时的注册拦截器,这里是注册过滤器。 方式二:使用Server3.0规范中的@WebFilter注解进行实现。

🌟实现方式

方式一

自定义过滤类:其中的doFilter方法为编写过滤逻辑的方法。比如说在登录时过滤掉没有token的用户。 思路

  • 实现Filter类。位于java.servletx.filter包的
  • 定义过滤逻辑。
public class TokenFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //初始化
        Filter.super.init(filterConfig);
    }
    @Override
    public void doFilter(HttpServletRequest servletRequest, HttpServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //拦截逻辑
        String token = servletRequest.getHeader("Authorization");
            if (token == null || token.isEmpty()) {
                throw new MyException("登录失败");
            }
       
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
        //销毁
        Filter.super.destroy();
    }
}

定义过滤注册器:对自定义的TokenFilter进行过滤注册。 思路

  • 使用@Configuration+@Bean注解配置Bean管理。
  • 使用TokenFilter类型的FilterRegistrationBean。
  • 设置过滤路径。
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean<TokenFilter> filterRegistrationBean() {
        FilterRegistrationBean<TokenFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new TokenFilter());
        registrationBean.addUrlPatterns("/api/*");
        return registrationBean;
    }
}

方式二

启动类:添加@ServletComponentScan注解。

@SpringBootApplication
@ServletComponentScan
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class);
    }
}

自定义过滤器类:添加@WebFilter注解并实现Filter接口。

@WebFilter注解相关参数

参数名类型默认值描述
filterNameString""过滤器名称
valueString[]{}过滤器的URL模式
urlPatternsString[]{}过滤器的URL模式
servletNamesString[]{}过滤器的Servlet名称
dispatcherTypesDispatcherType[]{DispatcherType.REQUEST}过滤器的调度类型
asyncSupportedbooleanfalse是否支持异步调度
initParamsWebInitParam[]{}过滤器的初始化参数
@WebFilter(urlPatterns = "/api/*")
public class TokenFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //初始化
        Filter.super.init(filterConfig);
    }
    @Override
    public void doFilter(HttpServletRequest servletRequest, HttpServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //拦截逻辑
        String token = servletRequest.getHeader("Authorization");
            if (token == null || token.isEmpty()) {
                throw new MyException("登录失败");
            }
       
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
        //销毁
        Filter.super.destroy();
    }
}

🌟写在最后

有关于SpringBoot自定义过滤器的内容到此就结束了。感谢大家的阅读,希望大家在评论区对此部分内容散发讨论,便于学到更多的知识。


请添加图片描述