SpringBoot入门系列篇(七):SpringBoot中使用Filter

前情提要

web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Filter,因为相对于拦截和监听来说,有时候原生的还是比较好用的,现在就来简单的在SpringBoot中使用这些特殊类吧
好吧,上面这句话是复制粘贴前面的使用Servlet文章的前情提要,懒得写了,直接进入正题吧


使用Filter实例

在SpringBoot中使用Filter也有两种方式:注解注册Filter和代码注册,同样也分别进行举例:
通过注解的方式进行注册:
首先,创建一个Filter,并使用WebFilter注解进行修饰,表示该类是一个Filter,以便于启动类进行扫描的时候确认,代码如下:
package org.framework.demo.section1;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 在SpringBoot中通过注解注册的方式简单的使用Filter
 * @author chengxi
 */
@WebFilter(urlPatterns = "/*", filterName = "myfilter")
public class FileterController implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter初始化中");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("开始进行过滤处理");
        //调用该方法后,表示过滤器经过原来的url请求处理方法
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("Filter销毁中");
    }
}
然后创建一个启动类,该启动类中同样额外添加一个注解用于自动扫描指定包下(默认是与启动类同包下)的WebFilter/WebServlet/WebListener等特殊类,代码如下:
package org.springframework.demo.section;

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

/**
 * 启动类测试
 * @author chengxi
 */
@SpringBootApplication
//该注解会扫描相应的包
@ServletComponentScan
public class Main {

    public static void main(String[] args){

        SpringApplication.run(Main.class, args);
    }
}
然后启动该类,输入网址:localhost:8080/index,不管我们是否写了该url请求的处理方法,我们查看控制台输出将会发现,Filter已经被初始化并且被使用了
接着,我们来通过代码注册的方式来使用Filter:
首先,创建一个Filter类,该类不使用WebFilter进行修饰:
package org.framework.demo.section1;

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

/**
 * 在SpringBoot中简单使用Filter
 * @author chengxi
 */
public class FileterController implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter初始化中");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("开始进行过滤处理");
        //调用该方法后,表示过滤器经过原来的url请求处理方法
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("Filter销毁中");
    }
}
然后,编写启动类,这里的启动类就不能直接使用注解ServletComponentScan来自动扫描了,需要我们手动添加代码来进行注册,需要用到的注册类是:FilterRegistrationBean,代码方式注册Filter代码如下:
package org.springframework.demo.section;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * 启动类测试
 * @author chengxi
 */
@SpringBootApplication
public class Main {

    /**
     * 代码方式注册Bean
     * @return
     */
    @Bean
    public FilterRegistrationBean setFilter(){

        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new FilterController());
        filterBean.setName("FilterController");
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }

    public static void main(String[] args){

        SpringApplication.run(Main.class, args);
    }
}
然后,我们启动该类,并输入网址:localhost:8080/index,同样打开控制台查看输出日志将会发现,该Filter也生效了


猜你喜欢

转载自blog.csdn.net/qq_27905183/article/details/79078561