学习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;

    }
}

测试结果

相关推荐
从零开始学习人工智能7 分钟前
SpringBoot + Apache Tika:一站式解决文件数据提取难题
spring boot·后端·apache
往事随风去8 分钟前
Java 中的 Consumer 与 Supplier 接口
java·面试·响应式编程
北城以北888816 分钟前
Java高级编程--XML
xml·java·开发语言·intellij-idea
SXJR32 分钟前
Java mp4parser 实现视频mp4 切割
java·python·音视频
冬天vs不冷38 分钟前
Java基础(十一):关键字final详解
java·开发语言
上官浩仁40 分钟前
springboot maven 多环境配置入门与实战
java·spring boot·maven
元直数字电路验证43 分钟前
新建Jakarta EE项目,Maven Archetype 选项无法加载出内容该怎么办?
java·maven
我叫汪枫1 小时前
Spring Boot图片验证码功能实现详解 - 从零开始到完美运行
java·前端·javascript·css·算法·html
小王不爱笑1321 小时前
Java基础知识(十四)
java·windows·python
现代科技改变命运1 小时前
泛型的学习
学习