学习spring boot-拦截器Interceptor,过滤器Filter

目录

拦截器Interceptor

过滤器Filter

关于过滤器的前置知识可以参考:

过滤器在springboot项目的应用

[一,使用@Webfilter+@ServletComponentScan 注解](#一,使用@Webfilter+@ServletComponentScan 注解)

[1 创建过滤器类实现Filter接口](#1 创建过滤器类实现Filter接口)

[2 在启动类中添加 @ServletComponentScan 注解](#2 在启动类中添加 @ServletComponentScan 注解)

二,创建过滤器配置类,注册过滤器

[1 创建过滤器类(不添加WebFilter注解)](#1 创建过滤器类(不添加WebFilter注解))

[2创建配置类, 注册过滤器类](#2创建配置类, 注册过滤器类)


拦截器Interceptor

可以参考 springmvc-拦截器-CSDN博客

过滤器Filter

关于过滤器的前置知识可以参考:

javaWeb之过滤器(Filter)_javaweb中filter过滤器对象什么时候创建?-CSDN博客

过滤器在springboot项目的应用

一,使用@Webfilter+@ServletComponentScan 注解
1 创建过滤器类实现Filter接口
  • 指定过滤器顺序(补充)。若存在多个过滤器且执行顺序重要,可使用@Order注解指定顺序。数字越小优先级越高。如@Order(1) 。
java 复制代码
package com.it.heima.springboot06filter.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;
@WebFilter("/user/*")
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("正在初始化~~~~~~");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("是否满足条件");
        System.out.println("如满足条件放行~~~~~~~~");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("正在销毁~~~~~~");

    }
}
2 在启动类中添加 @ServletComponentScan 注解
  • 扫描WebFilter注解,让该注解生效
java 复制代码
package com.it.heima.springboot06filter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan("com.it.heima.springboot06filter.filter")
public class Springboot06FilterApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot06FilterApplication.class, args);
    }

}

测试结果

java 复制代码
package com.it.heima.springboot06filter.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @RequestMapping("test")
    public String test(){
        System.out.println("正在执行test方法");
        return "test";
    }
}

二,创建过滤器配置类,注册过滤器
1 创建过滤器类(不添加WebFilter注解)
java 复制代码
package com.it.heima.springboot06filter.filter;

import jakarta.servlet.*;

import java.io.IOException;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("正在初始化~~~~~~");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("是否满足条件");
        System.out.println("如满足条件放行~~~~~~~~");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("正在销毁~~~~~~");

    }
}
2创建配置类, 注册过滤器类
java 复制代码
package com.it.heima.springboot06filter.config;

import com.it.heima.springboot06filter.filter.UserFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class FilterConfig implements WebMvcConfigurer {
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        // 创建过滤器注册对象
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        // 把过滤器注入到过滤器注册对象中
        filterRegistrationBean.setFilter(new UserFilter());
        // 设置过滤器拦截的请求
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;

    }
}

测试结果

相关推荐
ZHang......23 分钟前
LeetCode 1114. 按序打印
java·开发语言·算法
程序员欣宸24 分钟前
LangChain4j实战之四:集成到spring-boot
java·人工智能·spring boot
协同生态27 分钟前
天锐绿盾新版注册机【仅用于个人学习,禁止其他用途】
学习
慧都小项30 分钟前
Parasoft Jtest 如何用 JSON 文件驱动Java 测试自动化
java·自动化·json
Cuby!35 分钟前
【AFDM与信号处理:论文阅读】仿射频分复用:扩展OFDM以实现场景灵活性和弹性
论文阅读·笔记·学习·信息与通信·信号处理
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue酒店管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Hui Baby1 小时前
全局事务入口感知子事务方法-TCC
java·开发语言·数据库
AAA阿giao1 小时前
从零开始学 React:用搭积木的方式构建你的第一个网页!
前端·javascript·学习·react.js·前端框架·vite·jsx
Arciab1 小时前
C++ 学习_流程控制
c++·学习
HyperAI超神经1 小时前
【vLLM 学习】vLLM TPU 分析
开发语言·人工智能·python·学习·大语言模型·vllm·gpu编程